Android开发实现调用相本图片并裁剪上传功能
Android开发实现调用相册图片并裁剪上传功能
该功能用到了Apache的开源项目simplecropimage.
下面代码中CropImage类为SimpleCropImage中的类,该类需要在Manifest中进行注册。simplecropimage用到的依赖类在附件中。
直接上代码:
该功能用到了Apache的开源项目simplecropimage.
下面代码中CropImage类为SimpleCropImage中的类,该类需要在Manifest中进行注册。simplecropimage用到的依赖类在附件中。
直接上代码:
/** 打开相册(第一步)*/ private static final String IMAGE_UNSPECIFIED = "image/*"; private void openPhotos() { Intent intent = new Intent(Intent.ACTION_PICK); // intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT); intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED); startActivityForResult(intent, REQUEST_PHOTOS); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case REQUEST_CUTRESULT: dealCutResult(resultCode, data); break; case REQUEST_PHOTOS: dealPhotosResult(resultCode, data); break; } private void dealPhotosResult(int resultCode, Intent data) { if (Activity.RESULT_OK == resultCode) { if (data != null) { // 从相册获取照片uri startPhotoCut(data.getData()); } } else { if(GlobleParams.isEnglish){ shortToast("Upload canceled"); }else{ shortToast("您已经取消了上传操作"); } } } /**开始裁剪(第二步)*/ public void startPhotoCut(Uri uri) { String realPathFromURI = getRealPathFromURI(uri); if(realPathFromURI == null){ return; } File file = new File(realPathFromURI); Intent intent = new Intent(App.getInstance(), CropImage.class); // tell CropImage activity to look for image to crop intent.putExtra(CropImage.IMAGE_PATH, file.getAbsolutePath()); // allow CropImage activity to rescale image intent.putExtra(CropImage.SCALE, true); // if the aspect ratio is fixed to ratio 3/2 intent.putExtra(CropImage.ASPECT_X, 0); intent.putExtra(CropImage.ASPECT_Y, 0); // start activity CropImage with certain request code and listen // for result startActivityForResult(intent, REQUEST_CUTRESULT); } private void dealCutResult(int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK) { String path = data.getStringExtra(CropImage.IMAGE_PATH); // if nothing received if (path == null) {return;} uploadPhoto(path); } else { if(GlobleParams.isEnglish){ shortToast("Upload canceled"); }else{ shortToast("您已经取消了上传操作"); } } } /**开始上传(第三步)*/ public void uploadPhoto(String path) { File file = new File(path); MyLog.i("wmm", "file path" + file.getAbsolutePath() + "size " + file.getName() + " " + file.length()); final UserInfo user = App.getInstance().getUser(); MyLog.i("wmm", user.toString()); Bitmap photo = BitmapFactory.decodeFile(file.getPath()); iv_head_icon.setImageBitmap (BitmapUtil.getRoundedCornerBitmap(photo)); if (user != null) { Api.uploadavatar(App.getInstance(), user.username, user.password, path, new DefaultResponsehandler() { @Override public void onSuccess(int statusCode, Header[] headers, JSONObject response) { super.onSuccess(statusCode, headers, response); MyLog.i("wmm", "uploadPhoto onSuccess " + response.toString()); Api.loadDrawable(user.head_icon + "?time=" + System.currentTimeMillis(), iv_head_icon, R.drawable.default_head_icon); // Picasso.with(App.getInstance()).load(user.head_icon).skipMemoryCache().placeholder(R.drawable.default_head_icon).into(iv_head_icon); } @Override public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) { super.onFailure(statusCode, headers, responseString, throwable); MyLog.i("wmm", "uploadPhoto onFailure " + responseString.toString()); } @Override public void onSuccess(int statusCode, Header[] headers, JSONArray response) { super.onSuccess(statusCode, headers, response); MyLog.i("wmm", "uploadPhoto onSuccess 1" + response.toString()); } @Override public void onSuccess(int statusCode, Header[] headers, String responseString) { super.onSuccess(statusCode, headers, responseString); MyLog.i("wmm", "uploadPhoto onSuccess 2" + responseString.toString()); } @Override public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) { super.onFailure(statusCode, headers, throwable, errorResponse); MyLog.i("wmm", "uploadPhoto onFailure 1 " + statusCode + " " + throwable.getMessage()); } @Override public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONArray errorResponse) { super.onFailure(statusCode, headers, throwable, errorResponse); MyLog.i("wmm", "uploadPhoto onFailure 2 " + errorResponse.toString()); } @Override public void onFinish() { super.onFinish(); MyLog.i("wmm", "upload onFinish"); } @Override public void onStart() { super.onStart(); MyLog.i("wmm", "upload onStart"); } }); } } public String getRealPathFromURI(Uri contentUri) { String res = null; String[] proj = {MediaStore.Images.Media.DATA}; Cursor cursor = getActivity().getContentResolver().query(contentUri, proj, null, null, null); if (cursor != null && cursor.moveToFirst()) { int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); res = cursor.getString(column_index); } if(cursor != null){ cursor.close(); } return res; }