我正在尝试拍照并且我一直收到IllegalArgumentException,错误无法找到已配置的root。这是什么意思,我该如何解决?
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.fileprovidertest, PID: 3841
java.lang.IllegalArgumentException: Failed to find configured root that contains /data/data/com.example.fileprovidertest/files/JPEG_20170215_202539_1361683671.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.fileprovidertest.MainActivity.dispatchTakePictureIntent(MainActivity.java:126)
at com.example.fileprovidertest.MainActivity.onOptionsItemSelected(MainActivity.java:76)
at android.app.Activity.onMenuItemSelected(Activity.java:3204)
at com.android.internal.policy.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1215)
at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:761)
at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:904)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:894)
at android.widget.ActionMenuView.invokeItem(ActionMenuView.java:616)
at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:152)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
此功能发生异常:
private void dispatchTakePictureIntent() {
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) {
// Error occurred while creating the File
Toast.makeText(this, "File could not be created",
Toast.LENGTH_SHORT).show();
ex.printStackTrace();
}
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.fileprovidertest.fileprovider",
photoFile);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
此行发生错误:
Uri photoURI = FileProvider.getUriForFile(this,
"com.example.fileprovidertest.fileprovider",
photoFile);
以下是设置photoFile的功能:
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 storageDir = new File(getBaseContext().getCacheDir(), Environment.DIRECTORY_PICTURES);
// File storageDir = Environment.getExternalStorageDirectory();
File storageDir = getFilesDir();
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:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.fileprovidertest">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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.fileprovidertest.fileprovider"
android:grantUriPermissions="true"
android:exported="false">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths" />
</provider>
</application>
<uses-feature android:name="android.hardware.camera"
android:required="true" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
</manifest>
这是我的filepaths.xml:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images"
path="/data/data/com.example.fileprovidertest/files/" />
</paths>
我想我需要更改文件路径中的路径,但我不确定要将其更改为。
答案 0 :(得分:0)
根据Android documentation,添加filepaths.xml
:
确保将com.example.package.name替换为实际值 您应用的套餐名称。
如果您查看错误,它会告诉您由于某种原因它未能写入以下位置/data/data/com.example.fileprovidertest/files/...
。
由于您已在com.example.fileprovidertest.fileprovider
和“活动”中提供了路径AndroidManifest
,因此我怀疑您的.xml
应该遵循套件:
<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<external-path name="my_images"
path="/data/data/com.example.fileprovidertest.fileprovider/files/" />
</paths>