我创建了一个简单的活动。该活动允许用户从图库中选择图像或使用相机拍照。主要是它的工作正常,但是如果用户选择的图像有点大,我得到 JavaBinder(9581):!!!失败的BINDER TRANSACTION !! logcat错误,活动关闭。
方法:
private void choosePhoto(){
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
private void takePhoto(View view){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, TAKE_PICTURE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
try {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
int rotationValue = ImageController.getCameraPhotoOrientation(getApplicationContext(), selectedImage, picturePath);
Matrix m = new Matrix();
m.postRotate(rotationValue);
cursor.close();
decodeImageFile(picturePath, m);
}
if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK) {
imageUri = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
if (imageUri != null) {
Cursor cursor = getContentResolver().query(imageUri, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String cameraPath = cursor.getString(columnIndex);
cursor.close();
Matrix m = new Matrix();
int rotationValue = ImageController.getCameraPhotoOrientation(getApplicationContext(), imageUri, cameraPath);
m.postRotate(rotationValue);
decodeImageFile(cameraPath, m);
} else {
// Bundle extras = data.getExtras();
Matrix m = new Matrix();
int rotationValue = ImageController.getCameraPhotoOrientation(getApplicationContext(), imageUri, data.getExtras().get("data").toString());
m.postRotate(rotationValue);
// Need to work out what to do about the Matrix flip in the
// bug
// for Nexus5 Cameras.
nexus5CameraBugFix((Bitmap) data.getExtras().get("data"), m);
}
}
} catch (Exception e) {
Log.e("Exception", e.toString());
retakePhotoDialog();
}
}
logcat的:
11-26 12:31:27.441: E/JavaBinder(9581): !!! FAILED BINDER TRANSACTION !!!
我哪里错了?为什么我收到错误?
答案 0 :(得分:0)
你应该为你得到的每个大位图压缩你的位图(来自gallery,来自netowrk)。 以下是从图库中选择图片时始终使用的代码片段:
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(
ctx.getContentResolver().openInputStream(selectedImage), null,
o);
// The new size we want to scale to
final int REQUIRED_WIDTH = 300;
// Find the correct scale value. It should be the power of 2.
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_WIDTH
|| height_tmp / 2 < REQUIRED_WIDTH) {
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(
ctx.getContentResolver().openInputStream(selectedImage), null,
o2);
在上面的代码中,selectedImage是指向所选图像的URI点。
答案 1 :(得分:0)
你可以通过这种方式缩小图像大小来解决它:
public static Bitmap scaleDownBitmap(Bitmap photo, int newHeight, Context context) {
final float densityMultiplier = context.getResources().getDisplayMetrics().density;
int height= (int) (newHeight*densityMultiplier);
int width= (int) (height * photo.getWidth()/((double) photo.getHeight()));
photo=Bitmap.createScaledBitmap(photo, width, height, true);
return photo;
}
选择newHeight并缩小图像。