Android 尝试使用意图发送二进制内容时出现TransactionTooLargeException

Android 尝试使用意图发送二进制内容时出现TransactionTooLargeException,android,android-intent,Android,Android Intent,我需要将二进制内容从我的应用程序发送到设备中打开该文件类型的任何应用程序 我遵循这些指示: 这是我的密码: 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_PERMI

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

我遵循这些指示:

这是我的密码:

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);
有几件事:

如果我将intent action从action_VIEW更改为action_SEND,则可能会得到错误的应用程序列表以打开我的文件 发送的操作似乎有效,但仅适用于较小的文件大小 intent.setDataAndType似乎在OS M及更低版本的设备上运行良好。在N上,我得到相同的TransactionTooLargeException 最后,我需要完成以下任务:

我已经保存了一个文件,它存储在file:///storage/emulated/0/Download/TempFile.html 由于文件可能太大,我只需要将文件的位置发送给第三方应用程序(如adobe pdf reader)即可打开文件 M或更低版本无问题,N版本有大量问题 你知道我做错了什么吗?几天来我一直在试图解决这个问题,我读了大量的教程和建议,但没有最终的答案

谢谢

编辑:这是我的stacktrace:

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)
编辑2:从基本片段添加onSaveInstanceState代码:

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

您发布的logcat中的异常是由于将过多数据打包到onSaveInstanceState处理的包中而导致的

如果构建版本为N或更高,则以下是来自抛出它的Android源代码的:

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
        }
    }
}

我以回复的形式发布这篇文章,因为评论太长了。这并不是对问题的完整回答,因为问题中没有足够的信息来回答它。

您的意图不应该接近1MB IPC事务限制,因为流的实际大小不算在内。您可以编辑您的问题并发布整个Java堆栈跟踪吗?例如,使用类似的代码,我刚刚在运行安卓7.0的Nexus9上启动了一个~6MB MP4文件的ACTION_视图,没有任何问题。感谢您的帮助,againACTION\u视图不使用额外的\u流。ACTION\u视图将使用setDataAndType。操作\u发送使用额外的\u流。所有这些都不能解释堆栈跟踪,但您的代码甚至不应该走那么远。这是什么类型的文件,以及您尝试查看它时使用的应用程序是什么?在这一点上,我建议您尝试创建一个单独的独立项目来复制该问题。作为构建示例项目的一部分,您可能会确定主项目中的实际原因。如果你很难再现这个问题,这一点尤其正确,因为它会迫使你在示例中逐渐加入更多的主应用程序代码。如果您可以创建一个重现问题的示例,但没有发现问题的原因,请将该示例上载到某个位置,并从此处链接到该示例。数据包大小27943540以较小的块发送。我已经为这个类设置了断点,但是这里没有调用任何东西。但是,这是可能的,因为崩溃发生在基本片段类的onSaveInstanceState之后。您的片段和活动的onSaveInstanceState方法放在包中的是什么?发布代码。完成。子类未实现onSaveInstanceStateAnswer更新。很抱歉延迟,我病了一整天。最新的答案是什么?在stop方法中,我也没有一个断点来停止任何东西,所以这使得这更难。可以使用WebView,特别是html文件,但这取决于用户。我们还允许Chrome或其他浏览器打开同一文件。对于pdf和doc/docx文件,我们不使用WebView。谢谢你的帮助