0

当我在 Redmi 7A 上启动相机拍照时应用程序崩溃。也无法在 UncaughtException 处理程序中捕获崩溃。

捕获图像后,它停留在同一屏幕上,似乎没有崩溃,但所有数据都已清除。

Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File out = getActivity().getExternalFilesDir(null);
        filename = (System.currentTimeMillis() + ".jpg");
        out = new File(out, filename);
        if (Build.VERSION_CODES.N <= Build.VERSION.SDK_INT) {
            picUri = FileProvider.getUriForFile(getContext(), getContext().getApplicationContext().getPackageName() + ".provider", out);
            i.putExtra(MediaStore.EXTRA_OUTPUT, picUri);
        } else {
            i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(out));
        }
        getActivity().startActivityForResult(i, ConstantsUtils.CAMERA_REQUEST_CODE);

也在清单中使用 largeHeap 以获得足够的内存

android:largeHeap="true"

并且还在清单中添加了所需的功能

<uses-feature
    android:name="android.hardware.camera"
    android:required="true" />
<uses-feature
    android:name="android.hardware.camera.front"
    android:required="true" />

很抱歉,我没有找到有关此错误的任何日志,我们将不胜感激

4

1 回答 1

0

您必须使用 FileProvider。FileProvider 是 ContentProvider 的一个特殊子类,它允许通过 content URI 而不是 file:// URI 在应用程序之间共享文件。在 res/xml 文件夹中创建一个 XML 文件(provider_path.xml)并定义它 Manifest 文件 <provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.provider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/provider_paths"/> </provider>

在您的 provider_path.xml 文件中编写以下代码。 <?xml version="1.0" encoding="utf-8"?> <paths xmlns:android="https://schemas.android.com/apk/res/android"> <external-path name="external_files" path="."/> </paths>

于 2020-11-19T05:07:27.730 回答