【问题标题】:java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Picturesjava.lang.IllegalArgumentException:未能找到包含 /storage/emulated/0/Pictures 的已配置根
【发布时间】:2017-02-03 09:07:37
【问题描述】:

我正在尝试通过 Instagram 的 Intent 共享文件,但我意识到在 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:找不到配置的根目录 包含 /storage/emulated/0/Pictures/Polfaced/IMG_20170203_155130.jpg

尝试使用 commonware 的 StreamProvider,但我认为该库有不同的目的(修复另一种类型的问题),尝试将文件保存到 getFileDirs 而不是 Environment.getExternalStoragePublicDirectory 但图像未保存并出现类似错误/

【问题讨论】:

    标签: java android android-fileprovider


    【解决方案1】:

    改变

    <?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>
    

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

    以下提供者:

    <?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="."
    

    可以,请详细阅读您分享的链接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多