Android开发实现调用相册图片并裁剪上传功能

该功能用到了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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值