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

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

错误。 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
);

解决方案:

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

给TA打赏
共{{data.count}}人
人已打赏
解决方案

当从MVC控制器返回ActionResult时,JQuery Ajax post没有重定向。

2022-5-13 10:00:23

解决方案

在R中为每个独特的日期分配编号

2022-5-13 11:00:26

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索