gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:29:51 24 4
gpt4 key购买 nike

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

关注此https://inthecheesefactory.com/blog/how-to-share-access-to-file-with-fileprovider-on-android-nougat/en教程,我已经设置了提供者并提供这样的路径:

list

<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

尝试使用 commonware 的 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>

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

下面的提供者:

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

确实如此,请详细阅读您分享的链接。

关于java.lang.IllegalArgumentException : Failed to find configured root that contains/storage/emulated/0/Pictures 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42020506/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com