gpt4 book ai didi

java - 由于 java.lang.RuntimeException : android. os.TransactionTooLargeException 导致库崩溃:数据包大小 539544 字节

转载 作者:行者123 更新时间:2023-12-01 19:09:26 25 4
gpt4 key购买 nike

当我打开图库并选择图像应用程序时,会发生崩溃,并出现异常“java.lang.RuntimeException:android.os.TransactionTooLargeException:数据包大小539544字节”

代码如下

Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, SELECT_PHOTO_FROM_GALLERY);

以及 On Activity 结果方法

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()。和我已推荐

What to do on TransactionTooLargeException

http://nemanjakovacevic.net/blog/english/2015/03/24/yet-another-post-on-serializable-vs-parcelable/

最佳答案

您需要在设置为 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;
}

关于java - 由于 java.lang.RuntimeException : android. os.TransactionTooLargeException 导致库崩溃:数据包大小 539544 字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48661563/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com