我的目的是拍摄照片并保存。但如果我在 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)
...
最佳答案
I want to files_paths.xml dynamically read from string resources
抱歉,不支持该功能。
My file_paths.xml looks like :
如果您使用<external-files-path>
,您不需要 Android/data/.../files/
您的 path
的一部分值,以及那些 path
两种构建类型的值相同。
关于java - file_paths.xml 无法从字符串资源读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56706111/