使用 MediaStore.Images.Media 将位图保存在特定文件夹中

one*_*exf 5 android bitmap saving-data

我必须将在画布上绘制的位图保存在我自己的文件夹中。

String imgSaved = MediaStore.Images.Media.insertImage(
                    getContentResolver(), drawView.getDrawingCache(),
                    UUID.randomUUID().toString() + ".png", "drawing"); 
Run Code Online (Sandbox Code Playgroud)

我应该如何给出目录的路径?例如“/sdcard/MyPictures/”

Sar*_*ran 3

使用 Bitmap.compress 在所需位置另存为 JPG 或 PNG

File file = new File(yourpath, "yourfile.jpg");
FileOutputStream out = new FileOutputStream(filename);
yourbitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
Run Code Online (Sandbox Code Playgroud)

注意:其中的意思90是不压缩。它适用于 JPG,不适用于 PNG。不要忘记处理异常compression100