这是新的Android Q 范围存储的链接。
根据此Android开发者最佳实践博客,storing shared media files(这是我的情况)应使用MediaStore API完成。
深入研究文档,我找不到相关功能。
这是我在科特林的审判:
val bitmap = getImageBitmap() // I have a bitmap from a function or callback or whatever
val name = "example.png" // I have a name
val picturesDirectory = getExternalFilesDir(Environment.DIRECTORY_PICTURES)!!
// Make sure the directory "Android/data/com.mypackage.etc/files/Pictures" exists
if (!picturesDirectory.exists()) {
picturesDirectory.mkdirs()
}
try {
val out = FileOutputStream(File(picturesDirectory, name))
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)
out.flush()
out.close()
} catch(e: Exception) {
// handle the error
}
Run Code Online (Sandbox Code Playgroud)
结果是,Android/data/com.mypackage.etc/files/Pictures/example.png …
我的应用程序需要下载文件,我正在查看DownloadManager,但它有一些不适合我的情况(身份验证,命名方案,验证)的限制,所以我制作了自定义下载引擎.
是否可以手动将使用我的引擎下载的文件(因此使用本地URL)添加到下载系统应用程序的列表中?我的理解是列表由系统内容提供者填充.是否可以在没有DownloadManager尝试下载文件的情况下向其中添加记录?
谢谢 ;)