【解决】 Android Studio Failed to find configured root that contains

There was a problem

Trying to store the captured image to the custom storage space of the mobile phone, but it keeps reporting an error. The main problem is that an exception occurs when using the FileProvider to obtain the Uri from the file path.

related code

The code related to the file path in the error area
is mainly in the process of File imageDir = new File(getExternalCacheDir(), “Gallery”);, getExternalCacheDir() reported this exception.

File imageDir = new File(getExternalCacheDir(), "Gallery");
        if (!imageDir.exists()) {
    
    
            imageDir.mkdir();
         }
        mPhotoFile = new File(imageDir,"output_image.jpg");
        if(mPhotoFile.exists()){
    
    
            mPhotoFile.delete();
        }
        try {
    
    
            mPhotoFile.createNewFile();
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    
    
            mImageUri = FileProvider.getUriForFile(CameraActivity.this, getPackageName() + ".fileprovider", mPhotoFile);
        } else {
    
    
            mImageUri = Uri.fromFile(mPhotoFile);
        }

Configure FileProvider in AndroidManifest.xml
where ${applicationId} is the package name of the project, com.example.app, and the specific value can be found in build.gradle in the project module folder
@xml/file_paths: res/xml under the project path /file_paths.xml, file_paths is the file name

<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="${applicationId}.fileprovider"
            android:exported="false"
            android:grantUriPermissions="true">
            <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
</provider>

The content of file_paths.xml is as follows:
the name in each subtag refers to the name of the subfolder, and path refers to the path

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-files-path name="external" path="path" />
<!--代表app外部缓存区域的根目录,与Context.getExternalCacheDir()获取的路径对应。-->
    <external-cache-path name="Gallery" path="Android/data/com.example.app/files" />
</paths>

Analysis of the cause of the problem

Combined with the specific implementation of FileProvider.getUriForFile in the above java code, it is found that the corresponding folder path is: getExternalCacheDir()/path/name , so the following situation will be formed: getExternalCacheDir()/Android/data/com.example.app/files The path /Gallery obviously does not match File imageDir = new File(getExternalCacheDir(), “Gallery”); so an exception will be reported

problem solved

In fact, the most essential error is in the configuration of file_paths.xml. Change the external-cache-path in it to the following situation, and the problem will be solved. That is, path = '.' means that under the current path (the path is the path obtained by Context.getExternalCacheDir()), create a BEGallery subfolder, and path does not need to re-specify other paths.

<external-cache-path name="Gallery" path="." />

If you just want to specify the path in the path, you need to change the imageDir.mkdir(); in the code, change this to create a single folder, and change it to imageDir.mkdirs(); that is, create a multi-layer folder, which can also solve this problem .

Guess you like

Origin blog.csdn.net/qq_29750461/article/details/131742386