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

TransactionTooLargeException on API 28+ when sharing bitmap despite using Intent.EXTRA_STREAM

我正在尝试通过意图将位图共享给所有可以处理 images.Despite 的应用程序,并遵循了几个堆栈溢出答案中的说明,例如 this one and on android developers 我的代码在 [=47 上生成了 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)

源自我的 1. 方法的 Stacktrace:

    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" /> 添加到我的清单中

事实证明,您只需将其添加到 activity。 它清除了 Activity 的附属片段束。

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