使用Intent尝试发送二进制内容时,TransactionTooLargeException [英] TransactionTooLargeException when trying to send binary content, using Intents

查看:230
本文介绍了使用Intent尝试发送二进制内容时,TransactionTooLargeException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将二进制内容从我的应用程序发送到在设备中打开该文件类型的任何应用程序.

I need to send binary content from my app to whatever app opens that file type in the device.

我正在遵循以下说明: https://developer.android.com/training/sharing/send .html#send-binary-content

I'm following these instructions: https://developer.android.com/training/sharing/send.html#send-binary-content

这是我的代码:

final FileType fileType
final File file;

final Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);

intent.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(AncestryApplication.getAppContext(), AncestryApplication.getAppContext().getApplicationContext().getPackageName() + ".provider", file));

intent.setType(fileType.getMimeType());
startActivity(intent);

几件事:

  1. 如果我将意图操作从ACTION_VIEW更改为ACTION_SEND,则会显示错误的应用列表,以打开我的文件
  2. ACTION_SEND似乎有效,但仅适用于小文件
  3. intent.setDataAndType()在OS M和更低版本的设备上似乎可以正常工作.在N上,我得到相同的TransactionTooLargeException
  1. If I change the intent action from ACTION_VIEW to ACTION_SEND, I get the wrong possible list of apps to open my file
  2. ACTION_SEND seems to work, but only with small file sizes
  3. intent.setDataAndType() seems to work fine on devices OS M and lower. On N I get the same TransactionTooLargeException

最后,这是我需要完成的工作:

At the end, this is what I need to accomplish:

  • 我已经保存了一个文件,并将其存储在file:///storage/emulated/0/Download/TempFile.html
  • 由于文件可能太大,我只需要将文件的位置发送给第三方应用(例如Adobe PDF阅读器)即可打开文件
  • M或更低没有问题,N没问题很多

关于我可能做错了什么的任何想法?我一直在努力解决这一问题,并且我读了很多教程和SO建议,但没有任何最终答案.

Any ideas on what I may be doing wrong? I've been trying to solve this for a few days and I've read a ton of tutorials and SO suggestions, without any final answer.

谢谢.

这是我的堆栈跟踪:

AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapp.android.apps.myapp, PID: 26785
java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 27943540 bytes
  at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3752)
  at android.os.Handler.handleCallback(Handler.java:751)
  at android.os.Handler.dispatchMessage(Handler.java:95)
  at android.os.Looper.loop(Looper.java:154)
  at android.app.ActivityThread.main(ActivityThread.java:6077)
  at java.lang.reflect.Method.invoke(Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

Caused by: android.os.TransactionTooLargeException: data parcel size 27943540 bytes
  at android.os.BinderProxy.transactNative(Native Method)
  at android.os.BinderProxy.transact(Binder.java:615)
  at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:3606)
  at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3744)
  at android.os.Handler.handleCallback(Handler.java:751) 
  at android.os.Handler.dispatchMessage(Handler.java:95) 
  at android.os.Looper.loop(Looper.java:154) 
  at android.app.ActivityThread.main(ActivityThread.java:6077) 
  at java.lang.reflect.Method.invoke(Native Method) 
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

从基本片段中添加了onSaveInstanceState()代码:

EDIT 2: Added onSaveInstanceState() code from base fragment:

@Override
public void onSaveInstanceState(final Bundle outState) {
    mSaveInstanceStateCalled = true;
    outState.putBoolean(KEY_PROCESSING_SAVE, mSaveInProgress);
}

推荐答案

您发布的logcat中的异常是由于将太多数据打包到由onSaveInstanceState处理的Bundle中引起的.

The exception in the logcat you posted is caused by packing too much data into the Bundle processed by onSaveInstanceState.

这是方法:

private static class StopInfo implements Runnable {
    ActivityClientRecord activity;
    Bundle state;
    PersistableBundle persistentState;
    CharSequence description;
    @Override public void run() {
        // Tell activity manager we have been stopped.
        try {
            if (DEBUG_MEMORY_TRIM) Slog.v(TAG, "Reporting activity stopped: " + activity);
            ActivityManagerNative.getDefault().activityStopped(
                activity.token, state, persistentState, description);
        } catch (RemoteException ex) {
            if (ex instanceof TransactionTooLargeException
                    && activity.packageInfo.getTargetSdkVersion() < Build.VERSION_CODES.N) {
                Log.e(TAG, "App sent too much data in instance state, so it was ignored", ex);
                return;
            }
            throw ex.rethrowFromSystemServer(); // <<-- exception thrown here
        }
    }
}

我以答案的形式发布此消息,因为评论太长了.这并不是对问题的完整答案,因为问题中没有足够的信息来回答.

I am posting this in the form of an answer because it's too long for a comment. It is not meant as a complete answer to the question, because there is not enough information in the question to answer it.

这篇关于使用Intent尝试发送二进制内容时,TransactionTooLargeException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆