android - 无法保存图像并在我的画廊中查看
问题描述
我有一个要保存并显示在我的画廊中的位图图像,不幸的是,我在尝试完成此任务时遇到了很多问题。从昨天开始,我可能已经尝试了十几个不同的代码片段来做到这一点。我认为正在发生的事情是这些代码片段正在工作,但它们将其保存在应用程序的数据中,而不是共享存储/库中。
如何重新调整此代码以使其将图像存储在任何图库应用程序或文件管理器都可以访问的文件夹中,而不仅仅是在应用程序的数据中?
private fun saveToGallery(bitmap: Bitmap) {
Toast.makeText(applicationContext,"Saving image!", Toast.LENGTH_SHORT).show()
var outputStream: FileOutputStream? = null
val file = Environment.getExternalStorageDirectory()
val dir = File(file.absolutePath.toString() + "/MyPics")
dir.mkdirs()
val filename = String.format("%d.png", System.currentTimeMillis())
val outFile = File(dir, filename)
try {
outputStream = FileOutputStream(outFile)
} catch (e: Exception) {
e.printStackTrace()
}
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
try {
outputStream!!.flush()
} catch (e: Exception) {
e.printStackTrace()
}
try {
outputStream!!.close()
} catch (e: Exception) {
e.printStackTrace()
}
}
解决方案
您应该在保存图像后发送广播事件。
推荐阅读
- c# - 相当于将我的 newtonsoft 实现转换为 .net core 3.0 中的新 JSON 库期间的默认值
- excel - 从 FormControl ListBox 检索值时出现运行时错误
- angular - 如何为 Angular GET 请求设置验证码
- php - 获取每个数组中存在的嵌套数组的值
- python - 无法在 Python 中导入 Weka
- hibernate - 使用 @SpringBootTest 禁用数据库连接
- ios - 当标签中的值低于 Swift 4 中的阈值时,如何通过本地通知提醒用户?
- ios - Objective c scrollview屏幕截图在ios 13中有问题
- ios - WARNING-[UIApplication delegate] 只能在主线程中使用
- php - PHP函数获取数组值