悲剧的2.3让我费神了.居然用我之前的2.2代码调用了系统照相功能后,无法回调onActivityResult函数,原因是不能在调用前传路径,亲身经历,记录下来。
/**
* 选择上传的图片
*/
private void checkImage() {
final CharSequence[] items = { "相册", "拍照" };
Builder builder = new AlertDialog.Builder(ProductCommentActivity.this);
builder.setTitle(R.string.preferential_comment_checkimage_title).setItems(items, onClickListener);
builder.create().show();
}
/**
* 选择上传的图片
*/
private DialogInterface.OnClickListener onClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == 1) { // 拍照
Intent getImageByCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(getImageByCamera, CAMERAMAN_IMAGE);
} else {
Intent getImage = new Intent(Intent.ACTION_GET_CONTENT);
getImage.addCategory(Intent.CATEGORY_OPENABLE);
getImage.setType("image/*");
startActivityForResult(Intent.createChooser(getImage, "Select Picture"), SELECT_IMAGE);
}
}
};
/**
* 选择图片的回调函数
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Bitmap bitmap = null;
if (RESULT_OK == resultCode) {
if (requestCode == SELECT_IMAGE) { // 选择相册
bitmap = checkImage(data);
} else if (requestCode == CAMERAMAN_IMAGE) {// 照相
super.onActivityResult(requestCode, resultCode, data);
Date date = new Date(System.currentTimeMillis());
SimpleDateFormat dateFormat = new SimpleDateFormat("'IMG'_yyyy_MM_dd_HHmmss");
String imageName = dateFormat.format(date) + ".jpg";
Bundle extras = data.getExtras();
bitmap = (Bitmap) extras.get("data");
String fileParth =saveCommentImage(imageName, bitmap);
if (fileParth != null && !fileParth.equals("")) {
UPLOADIMAGE_PARTH = fileParth; //图片名称(个人项目需要)
}
}
}
if (bitmap != null) {
bitmap = convertBitmapPix(bitmap, commentImageWidth, commentImageHeight);
product_commentRatingUserIconImageView.setImageBitmap(bitmap);
}
}
/**
* 获取相册或SD卡选择的图片
*
* @param data
* @return
*/
@SuppressWarnings("finally")
private Bitmap checkImage(Intent data) {
Bitmap bitmap = null;
Cursor cursor = null;
try {
Uri originalUri = data.getData();
String path = originalUri.toString();
String[] proj = { MediaStore.Images.Media.DATA };
cursor = managedQuery(originalUri, proj, null, null, null);
if (cursor != null) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
// 最后根据索引值获取图片路径
path = cursor.getString(column_index);
cursor.close();
}
path = path.substring(path.indexOf("/sdcard"), path.length());
bitmap = FileHelper.getCommentImage(path);
if (bitmap != null) {
UPLOADIMAGE_PARTH = path;
}
} catch (Exception e) {
Log.e("checkImage", e.getMessage());
} finally {
if (cursor != null) cursor.close();
return bitmap;
}
}
/**
* 保存照相图片到系统默认相册
*
* @param name :图片名称
* @param bitmap :图片对象
* @return
*/
public static String saveCommentImage(String name, Bitmap bitmap) {
String parth = "";
try {
String status = Environment.getExternalStorageState();
if (status.equals(Environment.MEDIA_MOUNTED)) {
// 将图片保存到系统默认相册
String fileParth = Environment.getExternalStorageDirectory().getAbsolutePath() + Constant.IMAGE_FOLDER_NAME + name;
File myFile = new File(fileParth);
myFile.createNewFile();
FileOutputStream fOut = null;
fOut = new FileOutputStream(myFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
fOut.flush();
fOut.close();
parth = fileParth;
}
} catch (Exception e) {
Log.e("save Bitmap failed", e.getMessage());
}
return parth;
}
/**
* 转换图片大小
*
* @param bitmap :原始图片对象
* @param newWidth :新图片的宽度
* @param newHeight :新图片的高度
* @return
*/
@SuppressWarnings("finally")
public static Bitmap convertBitmapPix(Bitmap bitmap, int newWidth, int newHeight) {
Bitmap newBitmap = null;
try {
if (bitmap != null) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
// 计算缩放率
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 创建操作图片用的matrix对象
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 创建新的图片
newBitmap = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
}
} catch (Exception e) {
Log.e("convertBitmapPix", e.getMessage());
} finally {
return newBitmap;
}
}