//参考:https://blog.csdn.net/zz110753/article/details/60877594
TestService:
//第二种方式上传头像 @POST("file/upload") @Multipart Observable<Uptouxiang> upload2(@Query("uid") String uid, @Part MultipartBody.Part file); Model:
public class MyModle { public void GetData(String uid, File file, final GetBean getBean){ MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", file.getName(), RequestBody.create( MediaType.parse("application/octet-stream"), file)); Util.getmInstance().getNetData(Config.baseUri) .upload2(uid,filePart) .observeOn(AndroidSchedulers.mainThread()) .subscribeOn(Schedulers.newThread()) .subscribe(new Consumer<Uptouxiang>() { @Override public void accept(Uptouxiang uptouxiang) throws Exception { getBean.getBean(uptouxiang); } }); } public interface GetBean{ void getBean(Uptouxiang uptouxiang); } }//bean和修改昵称的bean类可以共用
//View
public interface Iview { void chenggong(String chenggong); void shibai(String shibai); }//presenter
public class MyPresenter { public MyModle myModle; public Iview iview; public MyPresenter(MyModle myModle, Iview iview) { this.myModle = myModle; this.iview = iview; } public void getMV(String uid, File file){ myModle.GetData(uid, file, new MyModle.GetBean() { @Override public void getBean(Uptouxiang uptouxiang) { String code = uptouxiang.getCode(); if(code.equals("0")){ iview.chenggong("上传成功"); }else { iview.shibai(uptouxiang.getMsg()); } } }); } }//MainActivity主要操作
ch_touxiang = findViewById(R.id.ch_touxiang); ch_touxiang.setOnClickListener(new View.OnClickListener() { private PopupWindow popupWindow; @Override public void onClick(View view) { View inflate = View.inflate(MainActivity.this, R.layout.popupwindow, null); popupWindow = new PopupWindow(inflate, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); //必须设置 popupWindow.setBackgroundDrawable(new ColorDrawable(getResources().getColor(android.R.color.transparent))); popupWindow.setOutsideTouchable(true); //显示在底部 main为activity_main布局控件中 最大的LinearLayout 的id popupWindow.showAtLocation(findViewById(R.id.main), Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0, 0); Button bt = inflate.findViewById(R.id.bt); Button bt1 = inflate.findViewById(R.id.bt1); Button bt2 = inflate.findViewById(R.id.bt2); bt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { autoObtainCameraPermission(); popupWindow.dismiss(); } }); bt1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { autoObtainStoragePermission(); popupWindow.dismiss(); } }); bt2.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { popupWindow.dismiss(); } }); }
protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { switch (requestCode) { case CODE_CAMERA_REQUEST://拍照完成回调 cropImageUri = Uri.fromFile(fileCropUri); PhotoUtils.cropImageUri(this, imageUri, cropImageUri, 1, 1, output_X, output_Y, CODE_RESULT_REQUEST); break; case CODE_GALLERY_REQUEST://访问相册完成回调 if (hasSdcard()) { cropImageUri = Uri.fromFile(fileCropUri); Uri newUri = Uri.parse(PhotoUtils.getPath(this, data.getData())); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) newUri = FileProvider.getUriForFile(this, "com.zz.fileprovider", new File(newUri.getPath())); PhotoUtils.cropImageUri(this, newUri, cropImageUri, 1, 1, output_X, output_Y, CODE_RESULT_REQUEST); } else { // ToastUtils.showShort(this, "设备没有SD卡!"); Tost("设备没有SD卡!"); } break; case CODE_RESULT_REQUEST: Bitmap bitmap = PhotoUtils.getBitmapFromUri(cropImageUri, this); if (bitmap != null) { //bitmap变成uri uri = Uri.parse(MediaStore.Images.Media.insertImage(this.getContentResolver(), bitmap, null, null)); MyPresenter myPresenter = new MyPresenter(new MyModle(), this); /** * 这里需要动态传uid */ Log.d("你猜哈", "onActivityResult: " + uid); myPresenter.getMV(uid, fileCropUri); // showImages(bitmap); } break; } } }
//上传头像 private static final int CODE_GALLERY_REQUEST = 0xa0; private static final int CODE_CAMERA_REQUEST = 0xa1; private static final int CODE_RESULT_REQUEST = 0xa2; private static final int CAMERA_PERMISSIONS_REQUEST_CODE = 0x03; private static final int STORAGE_PERMISSIONS_REQUEST_CODE = 0x04; //这里做了修改 private File fileUri = new File(Environment.getExternalStorageDirectory().getPath() + "/photo.jpg"); private File fileCropUri = new File(Environment.getExternalStorageDirectory(),"/crop_photo.jpg"); private Uri imageUri; private Uri cropImageUri; private Uri uri;