我知道我可以在 DCIM 中创建一个文件夹,如果其中有文件,则该目录将显示为专辑名称。我可以很好地创建该目录,在本例中,我将该目录称为“ThoughtCast”。
但是,我尝试保存 PNG 文件,但它没有出现。
这是我的代码:
public void savebitmap(Bitmap bmp) throws IOException {
String file_path =Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + "ThoughtCast/";
File dir = new File(file_path);
if(!dir.exists())
dir.mkdirs();
File file = new File(dir, "sketchpad" + ".png");
FileOutputStream fOut = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 85, fOut);
fOut.flush();
fOut.close();
}
任何帮助将不胜感激。谢谢
最佳答案
试试这个,它可能会帮助你(它是在 kotlin 中)
我目前正在执行此任务
private fun saveImage(bitmap: Bitmap) {
var outStream: FileOutputStream? = null
// Write to SD Card
try {
val dir = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + "ThoughtCast/")
dir.mkdirs()
val fileName = String.format("%s_%d.jpg", "Image", System.currentTimeMillis())
val outFile = File(dir, fileName)
outStream = FileOutputStream(outFile)
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream)
outStream.flush()
outStream.close()
Utils.showSnackBar(binding.rootView, getString(R.string.image_saved))
} catch (e: FileNotFoundException) {
Crashlytics.logException(e)
} catch (e: IOException) {
Crashlytics.logException(e)
} finally {
}
}
关于android - 如何在自定义图库中保存位图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51939069/