我正在打开相机来捕捉图像,并按照本教程进行操作。当我尝试为我的应用程序创建不同的风味构建配置时,标题中所述的错误出现了。
这是用于文件提供程序路径配置的名为file_paths的元数据 xml 资源文件 -
<?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 资源中的路径属性值设置为文字字符串而不是资源字符串值时,相同的代码不会导致任何异常。
我不明白为什么会这样。
如果我能更好地解释,请告诉我。请提前帮助和感谢。