我正在开发一个 Android 项目,但是当我需要从图库中拍照时,我被卡住了。确实,当我拍摄第一张照片时,一切都很顺利。但是,如果我拍摄第二张照片,则会引发异常“TransactionTooLargeException”并且我的应用程序崩溃了。
用于启动活动的代码:
public void addImage(View view) {
if(isPermissionEnable) {
try {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, ""), ADD_IMAGE);
} catch (Exception ex) {
GraphicUtils.displayPopup(this, getString(R.string.warning),
getString(R.string.image_too_large));
}
}else
{
askPermissions();
}
}
得到结果的代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case ADD_IMAGE:
if(getContentResolver().getType(data.getData()).contains("jpeg") || getContentResolver().getType(data.getData()).contains("jpg") || getContentResolver().getType(data.getData()).contains("png") || getContentResolver().getType(data.getData()).contains("bmp")) {
Uri uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
int minimumSize = (bitmap.getWidth() < bitmap.getHeight()) ? bitmap.getWidth() : bitmap.getHeight();
finalBitmap = ThumbnailUtils.extractThumbnail(bitmap, minimumSize, minimumSize);
imageView.setImageBitmap(finalBitmap);
button_right.setVisibility(View.VISIBLE);
button_left.setVisibility(View.VISIBLE);
} catch (Exception e) {
e.printStackTrace();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(uri.toString());
builder.setTitle("Erreur");
AlertDialog dialog = builder.create();
dialog.show();
}
}
else{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(getContentResolver().getType(data.getData()));
builder.setTitle("Erreur");
AlertDialog dialog = builder.create();
dialog.show();
}
break;
default :
break;
}
}
}
你有什么想法可以解决我的问题吗?