java.lang.IllegalArgumentException:找不到包含 /storage/emulated/0/Pictures 的已配置根目录

java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Pictures

我正在尝试通过 intent for instagram 共享文件,但我意识到在 API 24++ 上,我不能在未经许可的情况下将 URI 共享给其他应用程序。

按照这个 https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en 教程,我设置了提供程序并提供如下路径:

清单

<provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="myapplication.file_provider"
        android:exported="false"
        android:grantUriPermissions="true"
        tools:node="replace">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
    </provider>

注意:有 `tools:node=replace" 来覆盖库 RxPaparazzo 中的提供程序。

provider_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="files/"/>
</paths>

保存位图和获取uri的代码

public static Uri savePicture(Context context, Bitmap bitmap) {
        int cropHeight;
        if (bitmap.getHeight() > bitmap.getWidth()) cropHeight = bitmap.getWidth();
        else                                        cropHeight = bitmap.getHeight();

        bitmap = ThumbnailUtils.extractThumbnail(bitmap, cropHeight, cropHeight, ThumbnailUtils.OPTIONS_RECYCLE_INPUT);

        File mediaStorageDir = new File(
                Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                context.getString(R.string.app_name)
        );

        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                return null;
            }
        }

        String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        File mediaFile = new File(
                mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg"
        );

        // Saving the bitmap
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);

            FileOutputStream stream = new FileOutputStream(mediaFile);
            stream.write(out.toByteArray());
            stream.close();

        } catch (IOException exception) {
            exception.printStackTrace();
        }

        // Mediascanner need to scan for the image saved
        Intent mediaScannerIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        Uri fileContentUri = Uri.fromFile(mediaFile);
        mediaScannerIntent.setData(fileContentUri);
        context.sendBroadcast(mediaScannerIntent);

        return fileContentUri;
    }

编写文件提供程序 URI

Uri savedBitmap = ImageUtility.savePicture(getContext(), shareBitmap);
File media = new File(savedBitmap.getPath());
Uri contentUri = FileProvider.getUriForFile(getContext(), "myapplication.file_provider", media);

日志错误:

java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Pictures/Polfaced/IMG_20170203_155130.jpg

尝试使用普通软件的 StreamProvider,但我认为该库有不同的用途(解决另一种类型的问题),尝试将文件保存到 getFileDirs 而不是 Environment.getExternalStoragePublicDirectory,但图像没有保存并且变得相似错误/

改变

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

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

如您在上面分享的 link 中所述。

以下提供商:

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

将寻址类似于

的路径
/storage/emulated/0/files/

但是您共享的路径

/storage/emulated/0/Pictures/Polfaced/IMG_20170203_155130.jpg

不包括或开头为:

/storage/emulated/0/files/

了解一下

path="."

是吗,详细阅读你分享的link