方法getUriForFile-java.lang.IllegalArgumentException中出错:未能找到包含/storage/emulated/0/Android/data的已配置根目录/
我一直在开发一个简单的Android应用程序,它可以拍摄照片并将其存储在应用程序的目录中。我已经完全遵循了Android开发人员的教程,但仍然有一个错误 我在StackOverflow中看到了很多关于这个错误的问题,但是没有一个可以修复我的错误 这是我得到的logcat错误:方法getUriForFile-java.lang.IllegalArgumentException中出错:未能找到包含/storage/emulated/0/Android/data的已配置根目录/,java,android,android-camera,Java,Android,Android Camera,我一直在开发一个简单的Android应用程序,它可以拍摄照片并将其存储在应用程序的目录中。我已经完全遵循了Android开发人员的教程,但仍然有一个错误 我在StackOverflow中看到了很多关于这个错误的问题,但是没有一个可以修复我的错误 这是我得到的logcat错误: FATAL EXCEPTION: main Process: com.example.victormaruca.gpstesting, PID: 31539 java.lang.IllegalArgumentExc
FATAL EXCEPTION: main
Process: com.example.victormaruca.gpstesting, PID: 31539
java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.example.victormaruca.gpstesting/files/Pictures/JPEG_20170914_014854_-551864997.jpg
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:712)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:401)
at com.example.victormaruca.gpstesting.MainActivity.dispatchTakePictureIntent(MainActivity.java:124)
at com.example.victormaruca.gpstesting.MainActivity.access$000(MainActivity.java:43)
at com.example.victormaruca.gpstesting.MainActivity$3.onClick(MainActivity.java:81)
at android.view.View.performClick(View.java:5201)
at android.view.View$PerformClick.run(View.java:21163)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
这是我调用getUriForFile的方法:
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
Log.e("Erro", "no ceateImageFile() : "+ex);
}
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.victormaruca.gpstesting.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
这是我创建文件的方法:
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
mCurrentPhotoPath = image.getAbsolutePath();
return image;
}
AndroidManifest.xml代码段:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.victormaruca.gpstesting.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
</application>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera"
android:required="true" />
文件_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="Android/data/com.example.victormaruca.gpstesting/files/Pictures/" />
</paths>
任何帮助都将不胜感激 解决方案 将文件_paths.xml更改为
其他想法
我猜你是怎么看官方文章的。这里的示例res/xml/file_paths.xml令人困惑。例如:
<!-- Not working. According to the docs. -->
<external-files-path
name="my_images"
path="Android/data/com.example.package.name/files/Pictures" />
<!-- Works -->
<external-files-path
name="my_images"
path="Pictures" />
<!-- Also works -->
<external-files
name="my_images"
path="Android/data/com.example.package.name/files/Pictures" />
我找到了解决办法
另外,请看一下该方法的源代码
FileProvider$SimplePathStrategy.getUriForFile是您使用的文件。调试源代码时,您可以看到它是如何构建Uri的,以及它为什么会抛出IllegalArgumentException。它也可以带来一个解决方案
源代码:
...
// Find the most-specific root path
Map.Entry<String, File> mostSpecific = null;
for (Map.Entry<String, File> root : mRoots.entrySet()) {
final String rootPath = root.getValue().getPath();
if (path.startsWith(rootPath) && (mostSpecific == null
|| rootPath.length() > mostSpecific.getValue().getPath().length())) {
mostSpecific = root;
}
}
if (mostSpecific == null) {
throw new IllegalArgumentException(
"Failed to find configured root that contains " + path);
}
...
。。。
//查找最具体的根路径
Map.Entry mostSpecific=null;
对于(Map.Entry root:mRoots.entrySet()){
最后一个字符串rootPath=root.getValue().getPath();
if(path.startsWith(rootPath)&&(mostsspecific==null
||rootPath.length()>mostsspecific.getValue().getPath().length()){
MOST特异性=根;
}
}
if(mostSpecific==null){
抛出新的IllegalArgumentException(
找不到包含“+路径”的已配置根目录;
}
...
尝试添加Environment.getExternalStorageDirectory()和checkpath set context.getFilesDir().getPath();这将获取您的根目录path=“Android/data/com.example.victormaruca.gpstesting/files/Pictures/”
。更改为path=“。”
。尝试此操作可能有效:在您的文件中\u paths.xml检查此项。也请阅读这里的Commonware答案
...
// Find the most-specific root path
Map.Entry<String, File> mostSpecific = null;
for (Map.Entry<String, File> root : mRoots.entrySet()) {
final String rootPath = root.getValue().getPath();
if (path.startsWith(rootPath) && (mostSpecific == null
|| rootPath.length() > mostSpecific.getValue().getPath().length())) {
mostSpecific = root;
}
}
if (mostSpecific == null) {
throw new IllegalArgumentException(
"Failed to find configured root that contains " + path);
}
...