无法获得高质量图像:无法找到包含的已配置根目录

Failed to get high quality image: Failed to find configured root that contains

我正在尝试使用相机 Intent 捕获图像并将其发送到服务器。

我已经关注了官方文档https://developer.android.com/training/camera/photobasics

但是在使用 FileProvider 从文件路径获取 Uri 时出现异常。

我遇到异常

错误 -
未能找到包含 /storage/emulated/0/Android/data/com.example.capture/files/Pictures/20200703_162914_2747394966410567504.jpg

的已配置根目录

filepaths.xml

<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <cache-path name="my_images" path="."/>
</paths>

代码-JAVA

private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName =  timeStamp + "_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );

        // Save a file: path for use with ACTION_VIEW intents
        currentPhotoPath = image.getAbsolutePath();
        return image;
    }

    private void dispatchTakePictureIntent() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // Ensure that there's a camera activity to handle the intent
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            // Create the File where the photo should go
            File photoFile = null;
            try {
                photoFile = createImageFile();
            } catch (IOException ex) {
                Log.d("mylog", "Exception while creating file: " + ex.toString());
                // Error occurred while creating the File
            }
            // Continue only if the File was successfully created
            if (photoFile != null) {
                Log.d("mylog", photoFile.getAbsolutePath());
                Uri photoURI = FileProvider.getUriForFile(this,
                        "com.example.android.fileprovider",
                        photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, CAMERA_REQUEST);
            }
        }
    }

清单

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

您的 FileProvider 路径应指向文件所在的位置。

filepaths.xml 中更改:

<paths xmlns:android="http://schemas.android.com/apk/res/android"> 
    <cache-path name="my_images" path="."/>
</paths>

为此:

<paths xmlns:android="http://schemas.android.com/apk/res/android"> 
    <external-files-path name="Pictures" path="Android/data/com.example.capture/files/Pictures" />
</paths>