将 Flutter android (API 29) 应用程序下载的媒体图像、视频、PDF迁移到 Android 11API 级别 30的范围存储

问题描述

我们有一个类似于 Whatsapp(比如 MessagingApp)的 Flutter 应用程序,用于在封闭的人群中广播带有媒体(图像、视频、PDF)的消息。

当前 Flutter 应用的 android targetSdkVersion 为 29 并使用 Legacy Storage 使用文件路径访问方法来读写媒体文件,如下所述。

应用程序的所有媒体文件都下载在基本目录 /storage/emulated/0/MessagingApp 下。

以下是各种媒体的示例文件路径:

示例下载图像的路径 /storage/emulated/0/MessagingApp/MessagingApp_Images/20210410_111512.jpg

示例下载视频的路径 /storage/emulated/0/MessagingApp/MessagingApp_Videos/20210410_111513.mp4

示例下载的 PDF 的路径 /storage/emulated/0/MessagingApp/MessagingApp_Documents/20210410_11151.pdf

要下载图像,请使用 dio 包:

dio.download(imageHttpUrl,'/storage/emulated/0/MessagingApp/MessagingApp_Images/20210410_111512.jpg',)

下载的图像使用 Image.file 小部件在应用中呈现: Image.file(File('/storage/emulated/0/MessagingApp/MessagingApp_Images/20210410_111512.jpg'))

下载的图片可以从安卓的图库应用中查看。

从 2021 年 11 月开始,Playstore 强制 targetSdkVersion 为 30,并且在 API 级别 30 的范围存储模型中,应用无法直接在“共享存储”中创建文件夹。

在 Scoped Storage 中,可以从应用特定目录下载和使用文件 - /storage/emulated/0/Android/data/org.test.MessagingApp/files 但其他(图库)应用无法访问它们。

除此之外,Scoped Storage 还提到使用“MediaStoreAPI”来管理媒体。

如何使用 Android 11 的新 Scoped Storage 在 Flutter android (API 30) 中管理和处理下载的媒体?

我还注意到在运行 android 11 和 targetSdkVersion 30 的应用程序上的模拟器上,该应用程序无法在根存储 /storage/emulated/0/创建文件夹,但 android 文件夹“文档、下载、DCIM、图片等”是可自由访问,即可以使用现有代码在它们下创建新文件夹,即图像路径可以更改为 /storage/emulated/0/Documents/MessagingApp/MessagingApp_Images/20210410_111512.jpg 并且可以正常工作。

Flutter android 应用使用新的 Android 11 Scoped Storage 的最佳实践是什么?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)