即使文件大小超小,TransactionTooLargeException也是如此

时间:2018-01-25 22:49:11

标签: android android-intent android-sharing

尝试共享图片文件时,我一直收到此错误:

java.lang.RuntimeException:android.os.TransactionTooLargeException:data parcel size 1085992 bytes

我认为修复此问题的方法是将图像压缩得更多,这会减小尺寸。这是完成这项工作的功能:

 public static File saveBitmaptoFile(Bitmap bitmap, File pictureFile) {
    FileOutputStream out = null;
    try {
        out = new FileOutputStream(pictureFile);

        // on the next line I'm trying compress the heck out of image.
        bitmap.compress(Bitmap.CompressFormat.JPEG, 1, out);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        try {
            if (out != null) {
                out.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return pictureFile;
}

这是共享功能:

 private void shareToInstagram() {
        String type = "image/png";
        Intent share = new Intent(Intent.ACTION_SEND);
       //saveBitmpatoFile saves an extremely small and compressed file about 5kb in size

        File pictureFile = ImageUtil.saveBitmaptoFile(photo, ImageUtil.getOutputMediaFile());
        Uri imgUri = FileProvider.getUriForFile(mContext,"com.mycompany.myapp", pictureFile);

        share.setType(type);
        share.putExtra(Intent.EXTRA_STREAM, imgUri);
        mContext.startActivity(Intent.createChooser(share, "Share to"));
    }

我不明白,即使将这种极端压缩应用于图片文件,仍然会抛出TransactionTooLarge错误,说包裹大小实际上没有改变一位。更重要的是当我选择通过gmail共享文件时,我看到文件大小为5kb;我的文件位于文件中的1000kb的缓冲区大小之下!有人知道是什么导致这个错误仍然被抛出?

错误日志:

E/JavaBinder: !!! FAILED BINDER TRANSACTION !!!  (parcel size = 1085992)
W/ActivityThread: Bundle stats:
W/ActivityThread:   android:viewHierarchyState [size=3192]
W/ActivityThread:     android:views [size=3088]
W/ActivityThread:   android:support:fragments [size=5516]
W/ActivityThread: PersistableBundle stats:
W/ActivityThread:   [null]
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.myCompany.myApp, PID: 6837
              java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 1085992 bytes
                  at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3950)
                  at android.os.Handler.handleCallback(Handler.java:790)
                  at android.os.Handler.dispatchMessage(Handler.java:99)
                  at android.os.Looper.loop(Looper.java:164)
                  at android.app.ActivityThread.main(ActivityThread.java:6494)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
               Caused by: android.os.TransactionTooLargeException: data parcel size 1085992 bytes
                  at android.os.BinderProxy.transactNative(Native Method)
                  at android.os.BinderProxy.transact(Binder.java:764)
                  at android.app.IActivityManager$Stub$Proxy.activityStopped(IActivityManager.java:4623)
                  at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3934)
                  at android.os.Handler.handleCallback(Handler.java:790) 
                  at android.os.Handler.dispatchMessage(Handler.java:99) 
                  at android.os.Looper.loop(Looper.java:164) 
                  at android.app.ActivityThread.main(ActivityThread.java:6494) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
E/UncaughtException: java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 1085992 bytes
                     at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3950)
                     at android.os.Handler.handleCallback(Handler.java:790)
                     at android.os.Handler.dispatchMessage(Handler.java:99)
                     at android.os.Looper.loop(Looper.java:164)
                     at android.app.ActivityThread.main(ActivityThread.java:6494)
                     at java.lang.reflect.Method.invoke(Native Method)
                     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
                  Caused by: android.os.TransactionTooLargeException: data parcel size 1085992 bytes
                     at android.os.BinderProxy.transactNative(Native Method)
                     at android.os.BinderProxy.transact(Binder.java:764)
                     at android.app.IActivityManager$Stub$Proxy.activityStopped(IActivityManager.java:4623)
                     at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3934)
                     at android.os.Handler.handleCallback(Handler.java:790) 
                     at android.os.Handler.dispatchMessage(Handler.java:99) 
                     at android.os.Looper.loop(Looper.java:164) 
                     at android.app.ActivityThread.main(ActivityThread.java:6494) 
                     at java.lang.reflect.Method.invoke(Native Method) 
                     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

2 个答案:

答案 0 :(得分:15)

所以最终解决这个TransactionTooLarge异常的问题是识别具有从属片段,视图等的Activity,并将数据包添加到bundle中。然后我在所说的Activity中运行了这段代码:

@Override
protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    //Clear the Activity's bundle of the subsidiary fragments' bundles.
    outState.clear();
}

为我修好了。希望这能帮到那里的人!

答案 1 :(得分:0)

好的,这个答案还很晚,但是肯定会帮助在两个Activity之间传递日期的同时为TransactionTooLargeException找到可能解决方案的人。

我已经写了一个有关如何将大数据从一个Activity传递到另一个Activity的答案,该解决方案使用Shared Preference来存储Bundle并将其传递给另一个Activity。这是答案How to pass large data between two activity

注意:此解决方案在读取后清除存储的Bundle,并且如果重新创建Activity,则不会返回Bundle。这是一种解决方法,任何建议或问题将不胜感激。