java.lang.IllegalArgumentException: Failed to find configured root that contains “insert app directory path here”

大兔子大兔子 提交于 2019-12-11 07:02:07

问题


I am opening the camera to capture a image and i followed this tutorial. Error stated in the title comes when i tried to create different flavor builds configuration for my app.

This is my meta data xml resource file named file_paths for File Provider path configuration -

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" path="@string/images_file_path"/>
</paths>

I am setting it in manifest as follows -

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="@string/authority"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths" />
    </provider>

and i am instantiating string resource named "images_file_path" from gradle build file according to build congiguration like this -

productFlavors {
    dev {
        resValue "string", "images_file_path","\"Android/data/com.swinguff.android.dev/files/Pictures\""
    }
    prod {
        resValue "string", "images_file_path","\"Android/data/com.swinguff.android/files/Pictures\""
    }

and i am launching camera in my activity like this -

File photoFile = null;
        try {
            photoFile = createImageFile();
            if (photoFile != null) {
            /*Following line is throwing the error */
            profilePicUri =
            FileProvider.getUriForFile(ApplicationContext.getAppContext(),
                        BuildConfig.AUTHORITY,
                        photoFile);
                launchcamera();
                //Log.d("Musik","camera bug take photo"+photoFile+" "+mPhotoPathForCamera+" "+profilePicUri);
            }
        } catch (Exception ex) {
            // Error occurred while creating the File
            Log.d("CAMERA_BUG","exception "+ex.getMessage()+"\n"+BuildConfig.APPLICATION_ID+"\n"+BuildConfig.AUTHORITY
                +"\n"+getString(R.string.images_file_path));
            Util.showSnackbar(xRoot,getString(R.string.camera__open_error));

        }

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 */
    );

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

Same code does not cause any exception when i set value of path property in above meta data xml resource as a literal string and not by a resouce string value.

I don't understand why this is happening.

Please let me know if i can explain better.Please help and thanks in advance.


回答1:


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

see this edit your paths like this your problem will solve




回答2:


as Miller pointed out in the comments the solution is to put meta data xml resource file file_paths.xml separately for each build in their corresponding (dev/res/xml or prod/res/xml) folder and set corresponding path value directly in xml file and not by string resource images_file_path



来源:https://stackoverflow.com/questions/46359074/java-lang-illegalargumentexception-failed-to-find-configured-root-that-contains

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!