【问题标题】:java.lang.IllegalArgumentException: Failed to find configured root that contains "insert app directory path here"java.lang.IllegalArgumentException:找不到包含“在此处插入应用程序目录路径”的已配置根目录
【发布时间】:2017-09-22 07:25:12
【问题描述】:

我打开相机拍摄图像,我关注了this tutorial。当我尝试为我的应用程序创建不同的风味构建配置时,标题中所述的错误出现了。

这是我的元数据 xml 资源文件,名为 file_paths 用于文件提供程序路径配置 -

<?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>

我在清单中设置如下 -

    <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>

我正在根据这样的构建配置从 gradle 构建文件中实例化名为“images_file_path”的字符串资源 -

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\""
    }

我正在像这样在我的活动中启动相机 -

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;
}

当我将上述元数据 xml 资源中 path 属性的值设置为文字字符串而不是资源字符串值时,相同的代码不会导致任何异常。

我不明白为什么会这样。

如果我能更好地解释,请告诉我。请提前帮助和感谢。

【问题讨论】:

  • 您可以简单地将 file_paths.xml 文件放入您的风味路径中。像 dev/main/src/res/xml/file_paths.xml 和 prod/main/src/res/xml/file_paths.xml。并且只写完整路径而不使用字符串资源和 resValue 配置。
  • @Miller 非常感谢,您的解决方案有效......但我仍然不明白为什么它没有通过资源字符串正确设置
  • 使用字符串资源,您可以在您的 strings.xml 中覆盖此字符串资源以获得不同的风格。 gradle 配置中不需要 productFlavors 部分。我想这是问题的根源,编译器无法使用 gradle config 来解码字符串值。
  • @Miller 啊……好吧,我明白了……谢谢你的帮助

标签: android android-fileprovider android-flavors


【解决方案1】:

正如Miller 在 cmets 中指出的那样,解决方案是将元数据 xml 资源文件 file_paths.xml 分别为每个构建放在其对应的(dev/res/xml 或 prod/res/ xml) 文件夹并直接在 xml 文件中设置相应的路径值,而不是通过字符串资源 images_file_path

【讨论】:

    【解决方案2】:
    <paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="external_files" path="."/>
    </paths>
    

    看到这个编辑你的路径这样你的问题就会解决

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多