即使文件太小,TransactionTooLargeException [英] TransactionTooLargeException even when file size is super small
问题描述
尝试共享图像文件时,我不断收到此错误:
I keep getting this error when trying share an image file:
java.lang.RuntimeException:android.os.TransactionTooLargeException:数据包大小1085992字节
java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 1085992 bytes
我认为解决此问题的方法是将图像压缩得更多,从而减小尺寸。这是完成此工作的函数:
I assumed a fix for this would be to compress the image even more and that would lessen the size. Here's the function that does this job:
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的缓冲区大小!有人知道是什么原因导致仍然引发该错误吗?
I don't understand how, even with such extreme compression applied to the picture file, the TransactionTooLarge error still gets thrown, saying that the parcel size has effectively not changed one bit. What's more is when I select to share the file through gmail I see can that the file size is 5kb; I'm way below the buffer size of 1000kb sited in the docs! Anybody knows what could be causing this error to still be getting thrown?
错误日志:
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)
推荐答案
所以最终解决此TransactionTooLarge异常的目的是识别出其下级片段,视图等将数据包添加到包中。然后,我在上述活动中运行了这段代码:
So what it ultimately took resolve this TransactionTooLarge Exception, was to identify the Activity that had its subordinate fragments, views etc adding data parcels to the bundle. Then I ran this code in said Activity:
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//Clear the Activity's bundle of the subsidiary fragments' bundles.
outState.clear();
}
这对我来说很固定。希望这对外面的人有帮助!
That fixed it for me. Hope this help someone out there!
这篇关于即使文件太小,TransactionTooLargeException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!