[英]android.os.TransactionTooLargeException: data parcel size NOUGAT ERROR
[英]Crash in gallery due to java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 539544 bytes
当我打开画廊并选择一个图像应用程序时,出现异常"java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 539544 bytes"
代码如下
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, SELECT_PHOTO_FROM_GALLERY);
和在活动结果方法中
openDialog.dismiss();
try {
if (data == null || data.getData() == null) {
Toast.makeText(getContext(), "Error getting image.", Toast.LENGTH_SHORT).show();
return;
}
mUri = data.getData();
createFile(mUri, null);
} catch (Exception e) {
Log.e(TAG, "GALLERY EXCEPTION " + e.toString());
} catch (OutOfMemoryError E) {
Log.e(TAG, "GALLERY MEMORY EXCEPTION " + E.toString());
}
我没有使用onSavedInstancestate()
。 我已经退缩了
在TransactionTooLargeException上做什么
和
http://nemanjakovacevic.net/blog/english/2015/03/24/yet-another-post-on-serializable-vs-parcelable/
不要在服务和应用程序之间交换大量数据(> 1MB)。 我们无法通过意向大小> 1MB发送图像/数据。 当您尝试通过意图将大型位图图像/图像/ pojo从一个活动发送到另一个活动时,发生TransactionTooLargeException。
解决方案 :为此使用全局变量。
由于这个原因,您正在获得例外:
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, SELECT_PHOTO_FROM_GALLERY);
如果图像大小< 1MB
则可以使用,但我确定图像大小>1MB
。
在设置为imageview之前,您需要调整图像大小(如果图像很大,则需要在线程中调整图像大小)。
因此,您需要调用createFile(this,mUri)
,它将返回您的位图。 我已经对高度和宽度进行了硬编码,因此您可以更改自己。
/**
* Loads a bitmap and avoids using too much memory loading big images (e.g.: 2560*1920)
*/
private static Bitmap createFile(Context context, Uri theUri) {
Bitmap outputBitmap = null;
AssetFileDescriptor fileDescriptor;
try {
fileDescriptor = context.getContentResolver().openAssetFileDescriptor(theUri, "r");
BitmapFactory.Options options = new BitmapFactory.Options();
outputBitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);
options.inJustDecodeBounds = true;
int actualHeight = options.outHeight;
int actualWidth = options.outWidth;
float maxHeight = 740.0f;
float maxWidth = 1280.0f;
float imgRatio = actualWidth / actualHeight;
float maxRatio = maxWidth / maxHeight;
if (actualHeight > maxHeight || actualWidth > maxWidth) {
if (imgRatio < maxRatio) {
imgRatio = maxHeight / actualHeight;
actualWidth = (int) (imgRatio * actualWidth);
actualHeight = (int) maxHeight;
} else if (imgRatio > maxRatio) {
imgRatio = maxWidth / actualWidth;
actualHeight = (int) (imgRatio * actualHeight);
actualWidth = (int) maxWidth;
} else {
actualHeight = (int) maxHeight;
actualWidth = (int) maxWidth;
}
}
options.inSampleSize = calculateInSampleSize(options, actualWidth, actualHeight);
options.inJustDecodeBounds = false;
options.inTempStorage = new byte[16 * 1024];
outputBitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);
if (outputBitmap != null) {
Log.d(TAG, "Loaded image with sample size " + options.inSampleSize + "\t\t"
+ "Bitmap width: " + outputBitmap.getWidth()
+ "\theight: " + outputBitmap.getHeight());
}
fileDescriptor.close();
} catch (IOException e) {
e.printStackTrace();
}
return outputBitmap;
}
private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
final float totalPixels = width * height;
final float totalReqPixelsCap = reqWidth * reqHeight * 2;
while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) {
inSampleSize++;
}
return inSampleSize;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.