Android简单拍照java.lang.IllegalArgumentException:找不到配置的根目录

Android简单拍照java.lang.IllegalArgumentException:找不到配置的根目录,java,android,root,illegalargumentexception,Java,Android,Root,Illegalargumentexception,我正在尝试拍照,但不断收到IllegalArgumentException,错误为找不到配置的根目录。这意味着什么?我如何修复它 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.fileprovidertest, PID: 3841 java.lang.IllegalArgumentException: Failed to find configure

我正在尝试拍照,但不断收到IllegalArgumentException,错误为找不到配置的根目录。这意味着什么?我如何修复它

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);
以下是设置照片文件的功能:

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>

以下是我的filepath.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>

我想我需要更改文件路径中的路径,但我不确定要将其更改为什么。

根据Android,在添加
文件路径.xml时:

确保将com.example.package.name替换为实际的 应用程序的程序包名称

如果您查看错误,它会告诉您由于某种原因无法写入以下位置
/data/data/com.example.fileprovidertest/files/…

由于您在
AndroidManifest
和活动中都提供了路径
com.example.fileprovidertest.fileprovider
,我怀疑您的
.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>