相关疑难解决方法(0)

如何使用MediaStore在Android Q中保存图像?

这是新的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 …

java android image mediastore kotlin

13
推荐指数
3
解决办法
2680
查看次数

标签 统计

android ×1

image ×1

java ×1

kotlin ×1

mediastore ×1