简体   繁体   English

由于java.lang.RuntimeException而在图库中崩溃:android.os.TransactionTooLargeException:数据包大小539544字节

[英]Crash in gallery due to java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 539544 bytes

When I open gallery and select a image app get crash with the exception "java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 539544 bytes" 当我打开画廊并选择一个图像应用程序时,出现异常"java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 539544 bytes"

The code is as follow 代码如下

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

and in On activity result method 和在活动结果方法中

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());
}

I am not using onSavedInstancestate() . 我没有使用onSavedInstancestate() and I've reffered 我已经退缩了

What to do on TransactionTooLargeException 在TransactionTooLargeException上做什么

and

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

Do not exchange huge data (>1MB) between services and application. 不要在服务和应用程序之间交换大量数据(> 1MB)。 We can't send image/data through intent size > 1MB. 我们无法通过意向大小> 1MB发送图像/数据。 TransactionTooLargeException occurred when you tried to send large bitmap image/image/pojo from one activity to another via intent. 当您尝试通过意图将大型位图图像/图像/ pojo从一个活动发送到另一个活动时,发生TransactionTooLargeException。

Solution : Use global variable for this. 解决方案 :为此使用全局变量。

You are getting exception because of this: 由于这个原因,您正在获得例外:

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

If image size will < 1MB sure it will work, but I'm sure image size will be >1MB . 如果图像大小< 1MB则可以使用,但我确定图像大小>1MB

You need to resize your image size before set into imageview (if you having very large image then you need to resize your image in thread). 在设置为imageview之前,您需要调整图像大小(如果图像很大,则需要在线程中调整图像大小)。

So you need to call createFile(this,mUri) and it will return you bitmap. 因此,您需要调用createFile(this,mUri) ,它将返回您的位图。 I already put height and width hardcoded for now so you can change yourself. 我已经对高度和宽度进行了硬编码,因此您可以更改自己。

/**
 * 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.

相关问题 android.os.TransactionTooLargeException:数据包大小NOUGAT错误 - android.os.TransactionTooLargeException: data parcel size NOUGAT ERROR 未知错误:java.lang.RuntimeException android.os.Parcel.writeValue - Unknown Error : java.lang.RuntimeException android.os.Parcel.writeValue android-java.lang.RuntimeException - android - java.lang.RuntimeException android.os.TransactionTooLargeException检索已安装的应用程序 - android.os.TransactionTooLargeException retrieving installed applications 执行queryIntentActivities时android.os.TransactionTooLargeException - android.os.TransactionTooLargeException when executing queryIntentActivities java.lang.RuntimeException android - java.lang.RuntimeException android Android java.lang.RuntimeException - Android java.lang.RuntimeException Android 数据绑定错误:执行失败 java.lang.RuntimeException: - Android Data Binding Error:Execution failed java.lang.RuntimeException: 从 Fragment 移动到 Activity 导致 android.os.TransactionTooLargeException - Move from Fragment to Activity Causes android.os.TransactionTooLargeException 碎片化newInstance保存到捆绑的太多数据-android.os.TransactionTooLargeException - Fragment newInstance too much data saved to bundle - android.os.TransactionTooLargeException
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM