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

时间:2018-02-07 10:33:19

标签: java android crash

当我打开图库并选择图片应用时会遇到异常"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);

和On活动结果方法

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/

2 个答案:

答案 0 :(得分:0)

不要在服务和应用程序之间交换大量数据(> 1MB)。我们无法通过意图大小发送图像/数据> 1MB。当您尝试通过intent将大型位图图像/图像/ pojo从一个活动发送到另一个活动时发生TransactionTooLargeException。

解决方案:为此使用全局变量。

你因此而异常:

<build>
   <!-- ... -->

   <plugins>
   <plugin>
      <groupId>com.coderplus.maven.plugins</groupId>
      <artifactId>copy-rename-maven-plugin</artifactId>
      <version>1.0</version>
      <executions>
         <execution>
           <id>copy-my-jar</id>
           <phase>process-classes</phase>
           <goals>
             <goal>copy</goal>
           </goals>
           <configuration>
            <sourceFile>*filepath*</sourceFile>
            <destinationFile>${project.build.outputDirectory}/lib/my.jar</destinationFile>
           </configuration>
         </execution>
      <executions>
  </plugin>
  </plugins>
</build>

如果图片尺寸Intent intent = new Intent(Intent.ACTION_PICK); intent.setType("image/*"); startActivityForResult(intent, SELECT_PHOTO_FROM_GALLERY); 确定会有效,但我确定图片尺寸为< 1MB

答案 1 :(得分:0)

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