拍摄并保存图片:" IllegalArgumentException:无法找到包含"的已配置根目录

时间:2016-09-02 09:21:45

标签: android file android-camera

我尝试使用Google的培训部分" Taking photos simply"我正面临着一个问题。我复制了代码,只更改了项目相关数据:

在清单中:

// inside application tag
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${applicationId}.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths"/>
    </provider>

在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="Android/data/${applicationId}/files/Pictures" />
</paths>

拍照:

static final int REQUEST_TAKE_PHOTO = 1;
private String mCurrentPhotoPath;

protected void takeAPic() {

    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
            ex.printStackTrace();

        }
        // Continue only if the File was successfully created
        if (photoFile != null) {
            Uri photoURI = FileProvider.getUriForFile(this, // here the exception is triggered.
                    BuildConfig.APPLICATION_ID + ".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 = presenter.getGtin() + "-" + 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 = "file:" + image.getAbsolutePath();
    return image;
}

例外:

java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Android/data/my.package.name/files/Pictures/8712561249140-20160902_104505516416962.jpg
               at android.support.v4.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:679)
               at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:378)
               at my.package.name.usecases.reviewdata.ReviewDataActivity.takeAPic(ReviewDataActivity.java:242)
               at my.package.name.usecases.reviewdata.ReviewDataActivity.onOptionsItemSelected(ReviewDataActivity.java:118)
               at android.app.Activity.onMenuItemSelected(Activity.java:2885)
               at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:421)
               at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:188)
               at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:103)
               at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:103)
               at android.support.v7.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:69)
               at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:202)
               at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:761)
               at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:810)
               at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
               at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:957)
               at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:947)
               at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:618)
               at android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:155)
               at android.view.View.performClick(View.java:4789)
               at android.view.View$PerformClick.run(View.java:19881)
               at android.os.Handler.handleCallback(Handler.java:739)
               at android.os.Handler.dispatchMessage(Handler.java:95)
               at android.os.Looper.loop(Looper.java:135)
               at android.app.ActivityThread.main(ActivityThread.java:5294)
               at java.lang.reflect.Method.invoke(Native Method)
               at java.lang.reflect.Method.invoke(Method.java:372)
               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

我在这里搜索过,没有回答帮助我,因为他们是不同的案例。我做错了什么?我已经清理了项目,也卸载了它并尝试使用appID而不是gradle中的值。

非常感谢!

9月5日更新:我创建了一个新项目,以确保没有任何干扰它。同样的错误,也尝试了不同的手机和模拟器具有相同的结果。

1 个答案:

答案 0 :(得分:0)

路径的值可以是相对的:

<?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
    <external-path name="my_images" path="Pictures/" />
</paths>