Save bitmap to location

前端 未结 19 2426
悲哀的现实
悲哀的现实 2020-11-22 00:20

I am working on a function to download an image from a web server, display it on the screen, and if the user wishes to keep the image, save it on the SD card in a certain fo

19条回答
  •  北荒
    北荒 (楼主)
    2020-11-22 01:01

    Here is the sample code for saving bitmap to file :

    public static File savebitmap(Bitmap bmp) throws IOException {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.JPEG, 60, bytes);
        File f = new File(Environment.getExternalStorageDirectory()
                + File.separator + "testimage.jpg");
        f.createNewFile();
        FileOutputStream fo = new FileOutputStream(f);
        fo.write(bytes.toByteArray());
        fo.close();
        return f;
    }
    

    Now call this function to save the bitmap to internal memory.

    File newfile = savebitmap(bitmap);

    I hope it will help you. Happy codeing life.

提交回复
热议问题