FileProvider错误-无法找到包含以下内容的已配置根目录

时间:2018-11-12 14:35:31

标签: java android android-external-storage android-fileprovider

我有一个应用程序,在我的应用程序中,我需要拍照并保存在图库中。因此,我尝试了一些方法,但只保存了图像的缩略图。因此,我想要完整尺寸的照片,并且在android开发人员文档中说我必须用于此文件提供程序。 但是我遇到了这样的异常:java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/DCIM/JPEG_20181112_181825_2080717374.jpg

这是我的代码。如有任何疑问,欢迎))

public void takePicture() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    if (takePictureIntent.resolveActivity(getActivity().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(getActivity(),
                    "com.example.android.fileprovider",
                    photoFile);
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(takePictureIntent, REQUEST_CODE);
        }
    }
}


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 = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
    if (!storageDir.exists()) {
        storageDir.mkdir();
    }
    //File storageDir = new File(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;
}

好,这是拍照的过程。但是现在我想将其保存为图库(路径是这样的:/storage/emulated/0/DCIM)。因此,它给出了这样的错误:java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/DCIM/JPEG_20181112_181825_2080717374.jpg

这是清单中我的FileProvider:

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.example.android.fileprovider"
        android:exported="false"
        android:grantUriPermissions="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/file_paths"/>
</provider>

这是path.xml:

<paths>
<external-path name="my_images" path="storage/emulated/0/DCIM/" />
</paths>

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。因此,所有标签都已添加,因此您无需担心文件夹路径。将res / xml / file_paths.xml替换为:

NaN