//Retrofit依赖
compile 'com.squareup.retrofit2:retrofit:2.3.0'
//数据解析
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
//关联RXJava
compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
//RxJava依赖
compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
compile 'io.reactivex.rxjava2:rxjava:2.1.7'
compile 'org.greenrobot:greendao:3.2.2' // add library
public interface API {
//需要用户登录获得token值的时候需要登录
@FormUrlEncoded
@POST("user/login")
Observable> login(@Field("mobile") String mobile,
@Field("password") String password);
/**
* 文件上传
* @param url
* @param uid
* @param token
* @param img_part
* @return
*/
@Multipart //上传文件的注解
@POST("file/upload")
Observable upload(
@Part("uid") RequestBody uid,//用户ID
@Part MultipartBody.Part img_part);//图片文件
}
//BUtton点击事件
/**
* 相机拍照
*
* @param view
*/ public void camera_pic(View view) {
// 这个出捕获图片的常量值
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// 设置图片输出位置; 输出到制定的uri路径上;
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path)));
// 设置请求码
startActivityForResult(intent, 100);
}
/**
* 相册选取照片
*
* @param view
*/
public void picCollection_pic(View view) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 101);
}
//Intent回调
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//照相机
if (requestCode == 100 && resultCode == RESULT_OK) {
//拍照直接上传时用
Uri uri = Uri.fromFile(new File(path));
File file = new File(uri.getPath());
loginUpload("13834352233", "123456", file);
iv.setImageURI(uri);
//需要剪切时用
crop(Uri.fromFile(new File(path)));
//从相册直接选取照片回调
} else if (requestCode == 101 && resultCode == RESULT_OK) {
// 获取data中的数据
//拍照直接上传时用
Uri uri = data.getData();
//uri类型装换为String
String realPathFromURI = getRealPathFromURI(uri);
//再变为file类型
File file = new File(realPathFromURI);
loginUpload("13834352233", "123456", file);
iv.setImageURI(uri);
//需要剪切时用
crop(uri);
//剪切回调
} else if (requestCode == 102 && resultCode == RESULT_OK) {
//这个参数data,是固定写法
Bitmap bitmap = data.getParcelableExtra("data");
Uri uri = Uri.parse(MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, null,null));
//uri类型装换为String
String realPathFromURI = getRealPathFromURI(uri);
//再变为file类型
File file = new File(realPathFromURI);
loginUpload("13834352233", "123456", file);
iv.setImageBitmap(bitmap);
}
}
/**
* 剪裁是系统提供的一个应用,我们需要用intent
*
* @param uri
*/
public void crop(Uri uri) {
// 隐式启动, 里面传的是action
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setDataAndType(uri, "image/*");
intent.putExtra("crop", "true");
// 裁剪框的比例,1:1
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
// 裁剪后输出图片的尺寸大小
intent.putExtra("outputX", 250);
intent.putExtra("outputY", 250);
intent.putExtra("outputFormat", "JPEG");// 图片格式
intent.putExtra("noFaceDetection", false);// 取消人脸识别
// 设置剪裁后是否返回数据
intent.putExtra("return-data", true);
// 开启一个带有返回值的Activity,请求码为PHOTO_REQUEST_CUT
startActivityForResult(intent, 102);
}
//传入图片uri地址变为String
private String getRealPathFromURI(Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
CursorLoader loader = new CursorLoader(MainActivity.this, contentUri, proj, null, null, null);
Cursor cursor = loader.loadInBackground();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
//上传方法
public void loginUpload(String phone, String pwd, final File file) {
//TODO token 验证token
RetrofitHelper.getRetrofit(Urls.URSE).create(API.class).login( phone, pwd)
//TODO flatMap 应用场景???
.flatMap(new Function, ObservableSource>() {
@Override
public ObservableSource apply(HttpData userHttpData) throws Exception {
if (userHttpData != null) {
User user = userHttpData.data;
if (user != null) {
//UID
RequestBody uid = RequestBody.create(MediaType.parse("multipart/form-data"), user.uid);
//TOKEN
RequestBody token = RequestBody.create(MediaType.parse("multipart/form-data"), user.token);
RequestBody requestImgFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
// 创建MultipartBody.Part,用于封装文件数据
MultipartBody.Part requestImgPart =
MultipartBody.Part.createFormData("file", file.getName(), requestImgFile);
return RetrofitHelper.getRetrofit(Urls.UP).create(API.class).upload( uid, requestImgPart);
}
}
return Observable.empty();
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer() {
@Override
public void accept(HttpData httpData) throws Exception {
Toast.makeText(MainActivity.this, httpData.msg, Toast.LENGTH_SHORT).show();
}
});
}