问题描述
我正在尝试用相机拍照,但是出现以下错误:
I'm trying to take a picture with camera, but I'm getting the following error:
FATAL EXCEPTION: main
Process: com.example.marek.myapplication, PID: 6747
java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/com.example.marek.myapplication/files/Pictures/JPEG_20170228_175633_470124220.jpg
at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:711)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:400)
at com.example.marek.myapplication.MainActivity.dispatchTakePictureIntent(MainActivity.java:56)
at com.example.marek.myapplication.MainActivity.access$100(MainActivity.java:22)
at com.example.marek.myapplication.MainActivity$1.onClick(MainActivity.java:35)
AndroidManifest.xml:
AndroidManifest.xml:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.example.marek.myapplication.fileprovider"
android:enabled="true"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
Java:
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
Toast.makeText(getApplicationContext(), "Error while saving picture.", Toast.LENGTH_LONG).show();
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.marek.myapplication.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
file_paths.xml
file_paths.xml
<?xml version="1.0" encoding="utf-8"?>
<paths>
<files-path name="my_images" path="images/"/>
</paths>
我整天都在搜索有关此错误的内容,并试图理解FileProvider
,但是我不知道此错误消息试图告诉我什么.如果您需要更多信息/代码,请在评论中写信给我.
I was searching whole day about this error and trying to understand FileProvider
, but I have no idea what this error message tries to tell me. If you want more info/code, write me in the comment.
推荐答案
您的文件存储在getExternalFilesDir()
下.那将映射到<external-files-path>
,而不是<files-path>
.另外,您的文件路径中不包含images/
,因此XML中的path
属性无效.
Your file is stored under getExternalFilesDir()
. That maps to <external-files-path>
, not <files-path>
. Also, your file path does not contain images/
in it, so the path
attribute in your XML is invalid.
将res/xml/file_paths.xml
替换为:
<?xml version="1.0" encoding="utf-8"?>
<paths>
<external-files-path name="my_images" path="/" />
</paths>
更新2020年3月13日
特定路径的提供者路径如下:
Provider path for a specific path as followings:
-
<files-path/>
->Context.getFilesDir()
-
<cache-path/>
->Context.getCacheDir()
-
<external-path/>
->Environment.getExternalStorageDirectory()
-
<external-files-path/>
->Context.getExternalFilesDir(String)
-
<external-cache-path/>
->Context.getExternalCacheDir()
-
<external-media-path/>
->Context.getExternalMediaDirs()
<files-path/>
-->Context.getFilesDir()
<cache-path/>
-->Context.getCacheDir()
<external-path/>
-->Environment.getExternalStorageDirectory()
<external-files-path/>
-->Context.getExternalFilesDir(String)
<external-cache-path/>
-->Context.getExternalCacheDir()
<external-media-path/>
-->Context.getExternalMediaDirs()
参考: https://developer.android.com/reference/androidx/core/content/FileProvider
这篇关于FileProvider-IllegalArgumentException:无法找到配置的根目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!