简体   繁体   English

调用 TakePicture 时出现 Android TransactionTooLargeException

[英]Android TransactionTooLargeException when calling TakePicture

Need to utilise the camera in my app for work, I see that things have changed in API >= 28 compared to how I used to do it where I could use startActivityForResult.需要在我的应用程序中使用相机进行工作,我发现 API >= 28 与我以前可以使用 startActivityForResult 的方式相比发生了变化。

However I am facing a problem where I launch the camera app, and immediately get the 'TransactionTooLargeException' error message in the debug/run console.但是,我在启动相机应用程序时遇到问题,并立即在调试/运行控制台中收到“TransactionTooLargeException”错误消息。

For calling up the camera, I am doing为了调出相机,我正在做

mGetContent = registerForActivityResult(
            new ActivityResultContracts.TakePicture(),
            result -> {
                if (result) {

                }
            }
    );

Where mGetContent is defined in the class as其中 mGetContent 在 class 中定义为

private ActivityResultLauncher<Uri> mGetContent;

In my AndroidManifest.xml file I have the following在我的 AndroidManifest.xml 文件中,我有以下内容

<provider
        android:name="androidx.core.content.FileProvider"
        android:authorities="com.test.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>

In my file_paths file I have在我的 file_paths 文件中,我有

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <files-path
        name="files"
        path="."/>
    <files-path
        name="app_images"
        path="./files/"/>
</paths>

I have a button set up in my activity where I launch the camera using我在我的活动中设置了一个按钮,我使用它启动相机

findViewById(R.id.button)).setOnClickListener(v -> {
    File directory = new File(context.getFilesDir(), "app_images");
    if (!directory.exists()) directory.mkdir();

    File file = new File(directory, "image.jpg");

    Uri uri = getUriForFile(this, "com.test.fileprovider", file);

    mGetContent.launch(uri);
};

As soon as I tap on the button, and the camera app opens up, I get what I can only assume is an overly general error message.当我点击按钮并打开相机应用程序时,我会得到我只能假设是一个过于笼统的错误消息。

E/JavaBinder: !!! FAILED BINDER TRANSACTION !!!  (parcel size = 1284092)
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.test, PID: 14296
    java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 1284092 bytes
        at android.app.servertransaction.PendingTransactionActions$StopInfo.run(PendingTransactionActions.java:161)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7397)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)
     Caused by: android.os.TransactionTooLargeException: data parcel size 1284092 bytes
        at android.os.BinderProxy.transactNative(Native Method)
        at android.os.BinderProxy.transact(BinderProxy.java:511)
        at android.app.IActivityTaskManager$Stub$Proxy.activityStopped(IActivityTaskManager.java:4524)
        at android.app.servertransaction.PendingTransactionActions$StopInfo.run(PendingTransactionActions.java:145)

Been trying to Google search to find things, but having trouble nailing down what the actual issue is.一直在尝试用谷歌搜索来查找东西,但无法确定实际问题是什么。

Some suggestions pointed me towards the onSaveInstanceState, so I override that and set a breakpoint on it to see what was happening, but it made it through without any issues (from what I could tell).一些建议将我指向 onSaveInstanceState,因此我覆盖了它并在其上设置了一个断点以查看发生了什么,但它没有任何问题就通过了(据我所知)。

Kind of at a loss with this one.对这个有点不知所措。

Wowsers as to what makes up the bundle in the onSaveInstanceState.关于 onSaveInstanceState 中捆绑包的组成部分的哇塞。

I have some imageviews, imagebuttons, and just general buttons in my app to make things easier for our staff.我的应用程序中有一些图像视图、图像按钮和通用按钮,以使我们的员工更轻松。

I went through and changed the 'saveState' of all the ImageViews and ImageButtons from the default of true to false, since I don't care what state they were in, they are just visual guides.我将所有 ImageViews 和 ImageButtons 的“saveState”从默认的 true 更改为 false,因为我不在乎它们在什么 state 中,它们只是视觉指南。

Took the android:viewHierarchyState from 1.2MB down to 1.6KB, my Parcel size is now 3.3KB and it no longer crashes when suspending the app to bring up the camera app.将 android:viewHierarchyState 从 1.2MB 降低到 1.6KB,我的 Parcel 大小现在是 3.3KB,并且在暂停应用程序以调出相机应用程序时不再崩溃。

TooLargeTool was useful, but I couldn't make it work the way the Github page says, I told it to 'startLogging', and in my activity where the crash was happening, I set a breakpoint and checked if it was logging using 'isLogging' and it came back 'true'. TooLargeTool 很有用,但我无法让它像 Github 页面所说的那样工作,我告诉它“startLogging”,在发生崩溃的活动中,我设置了一个断点并检查它是否正在使用“isLogging”进行日志记录'然后它回来了'真实'。

In the end I just had it log the output of TooLargeTool.bundleBreakdown(outState) in the onSaveInstanceState.最后,我只是让它在 onSaveInstanceState 中记录了TooLargeTool.bundleBreakdown(outState)的 output。

Thanks to Gabe Sechan and ianhanniballake for pointing me towards what it might be, there's not much out there on for this particular exception, I mean, there is, but it appears that it is different for everyone.感谢 Gabe Sechan 和 ianhanniballake 向我指出它可能是什么,对于这个特殊的例外情况并没有太多,我的意思是,有,但似乎每个人都不同。

Really wish Google would print out a better set of error messages for it to make it easier to work out which activity was the problem (or in my case, all 3 activities combined).真的希望谷歌能打印出一组更好的错误消息,以便更容易确定哪个活动是问题(或者在我的情况下,所有 3 个活动结合起来)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM