Android 개발 - 앨범 이미지 호출 및 편집 및 업로드

7388 단어 Android
이 기능은 Apache의 소스 오픈 프로젝트인 단순cropimage를 사용합니다.
다음 코드의 CropImage 클래스는 Manifest에 등록해야 하는 SimpleCropImage의 클래스입니다.단순cropimage에서 사용하는 의존 클래스는 첨부 파일에 있습니다.
직접 부호:

/** ( )*/
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;
}

좋은 웹페이지 즐겨찾기