file_paths.xml无法从字符串资源中读取

时间:2019-06-21 15:12:57

标签: java android android-resources

我的目的是捕获照片并保存。但是,如果我在file_paths.xml上使用字符串生成器动态定义文件路径,则会引发应用抛出错误。

我有2种构建类型(测试,发布)。我想从字符串资源动态读取files_paths.xml。例如,我在build.gradle(模块应用程序)中有2种构建类型:

    release {
            ...
            resValue "string", "appName", "Easyloan"
            resValue 'string', 'images_file_path', "Android/data/humo.tj.easyloan/files/Pictures"
        }
        // local test server
        debug {
             ...
            resValue "string", "appName", "Test-Easyloan"
            resValue 'string', 'images_file_path', "Android/data/humo.tj.easyloantest/files/Pictures"
        }

我的file_paths.xml看起来像:

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

获取文件uri的Java代码

            if (photoFile != null) {
                photoURI = FileProvider.getUriForFile(getActivity(), BuildConfig.APPLICATION_ID+".fileprovider", photoFile);
                pictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
                        photoURI);
                startActivityForResult(pictureIntent,
                        REQUEST_CAPTURE_IMAGE);

AndroiManifest.xml:

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

但是尝试拍照时出现错误。错误消息:

2019-06-21 19:55:56.248 8275-8275/humo.tj.easyloantest E/AndroidRuntime: FATAL EXCEPTION: main
    Process: humo.tj.easyloantest, PID: 8275
    java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/humo.tj.easyloantest/files/Pictures/IMG_20190621_145556_1720187320.jpg
        at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:739)
        at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:418)
        at humo.tj.easyloan.fragmentsNewapplication.Fragment4.openCameraIntent(Fragment4.java:176)
        at humo.tj.easyloan.fragmentsNewapplication.Fragment4.lambda$null$0$Fragment4(Fragment4.java:100)
        at humo.tj.easyloan.fragmentsNewapplication.-$$Lambda$Fragment4$c1wFOH2hFscxkFmF0i4x39JUikc.onClick(lambda)
        at com.android.internal.app.AlertController$AlertParams$3.onItemClick(AlertController.java:1136)
        at android.widget.AdapterView.performItemClick(AdapterView.java:310)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1156)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:3121)
...

1 个答案:

答案 0 :(得分:1)

  

我想从字符串资源动态读取files_paths.xml

对不起,不支持。

  

我的file_paths.xml看起来像:

如果使用<external-files-path>,则不需要Android/data/.../files/值的path部分,并且两个构建类型的path值都相同。< / p>