尽管使用Intent.EXTRA_STREAM,但共享位图时,API 28+上的TransactionTooLargeException

时间:2019-06-02 15:24:59

标签: android android-intent android-bitmap

我试图通过一种意图与所有可以处理图像的应用程序共享一个位图。尽管按照this oneon android developers这样的堆栈溢出答案中的说明进行操作,我的代码仍会在API上生成为TransactionTooLargeException 28岁及以下的某些设备。即使我一直在使用Intent.EXTRA_STREAM,并且从不发送位图直接。

  1. 我的第一种简单方法:
         val intent = Intent(Intent.ACTION_SEND).apply {
                type = "image/jpeg"
                putExtra(Intent.EXTRA_STREAM, getImageUri(context!!, bitmap))
            }
            startActivity(Intent.createChooser(intent, "Share image"))



     fun getImageUri(inContext: Context, inImage: Bitmap): Uri {
    val bytes = ByteArrayOutputStream()
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes)
    val path = MediaStore.Images.Media.insertImage(inContext.contentResolver, inImage, "Title", null)
    return Uri.parse(path) }
  1. 我还尝试通过提供程序检索uri:

        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"
            tools:replace="android:resource" />
    </provider>
    

provider_paths.xml:

<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="external_files" path="."/>
<cache-path name="external_files" path="."/>
<cache-path name="shared_images" path="images/"/>
<external-path name="external_files" path="."/>
<external-files-path name="external_files" path="."/>
<external-cache-path name="external_files" path="."/>

获取uri,继续执行步骤1:

  val uri = FileProvider.getUriForFile(it.activity, "...", file)

Stacktrace源自我的方法1.

    2019-06-02 17:10:51.255 19592-19592/... E/JavaBinder: !!! FAILED BINDER TRANSACTION !!!  (parcel size = 9344028)
2019-06-02 17:10:51.292 19592-19592/... E/AndroidRuntime: FATAL EXCEPTION: main
    Process: ..., PID: 19592
    java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 9344028 bytes
        at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3998)
        at android.os.Handler.handleCallback(Handler.java:794)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:6635)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
     Caused by: android.os.TransactionTooLargeException: data parcel size 9344028 bytes
        at android.os.BinderProxy.transactNative(Native Method)
        at android.os.BinderProxy.transact(Binder.java:774)
        at android.app.IActivityManager$Stub$Proxy.activityStopped(IActivityManager.java:4723)
        at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3982)
        at android.os.Handler.handleCallback(Handler.java:794) 
        at android.os.Handler.dispatchMessage(Handler.java:99) 
        at android.os.Looper.loop(Looper.java:176) 
        at android.app.ActivityThread.main(ActivityThread.java:6635) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823) 

PS:我已将<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />添加到清单中

1 个答案:

答案 0 :(得分:0)

事实证明,您只需要将其添加到您的活动中即可。 它将清除活动的捆绑包的辅助片段的捆绑包。

 override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        //FIX: TransactionTooLargeException when sharing image via intent. Clear the Activity's bundle of the subsidiary fragments' bundles.
        outState.clear()
    }