Android.FileProvider.getUriForFile "找不到配置的根"。FileProvider.getUriForFile "未能找到配置的根"

问题描述 投票:0回答:1

我想实现一个相机活动,让用户拍照并保存到手机上。

错误。 java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/no.test.group_project/files/Pictures/JPEG_20200423_114453_750680396837474735.jpg

AndroidManifest.xml

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

file_paths.xml。

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-files-path name="my_images" path="Android/data/no.test/group_project/files/Pictures/" />
</paths>

Java

    public void openCamera(View view) {
        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) {
                ex.printStackTrace();
            }
            if (photoFile != null) {
                Uri photoURI = FileProvider.getUriForFile(this,
                        "no.test.group_project.fileprovider",
                        photoFile);
                takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
                startActivityForResult(takePictureIntent, Utils.REQUEST_IMAGE_CAPTURE);
            }
        }
    }
    private File createImageFile() throws IOException {
        // Create an image file name
        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        String imageFileName = "JPEG_" + timeStamp + "_";
        File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
        File image = File.createTempFile(
                imageFileName,  /* prefix */
                ".jpg",         /* suffix */
                storageDir      /* directory */
        );
        currentPhotoPath = image.getAbsolutePath();
        return image;
    }

当我试图从以下地方获取Uri时,我得到了以上的错误。

FileProvider.getUriForFile(this,
    "no.test.group_project.fileprovider",
    photoFile
);
java android xml android-fileprovider
1个回答
0
投票

替换 path="Android/data/asmund.thomas.group_project/files/Pictures/"path=".".

© www.soinside.com 2019 - 2024. All rights reserved.