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

时间:2017-02-03 09:07:38

标签: java android android-fileprovider

我尝试通过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&#34;从库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:找不到配置的root   包含   /storage/emulated/0/Pictures/Polfaced/IMG_20170203_155130.jpg

尝试使用普通软件的StreamProvider,但我认为该库有不同的用途(修复其他类型的问题),尝试将文件保存到getFileDirs而不是Environment.getExternalStoragePublicDirectory,但图像未保存,得到类似的错误/

1 个答案:

答案 0 :(得分:5)

更改

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

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