Store Bitmap image to SD Card in Android

前端 未结 3 1114
鱼传尺愫
鱼传尺愫 2021-01-05 19:01

I am facing some strange problem with my android code, I have an image in Bitmap variable and want to save that file to SD card. I code as follow,

Bitmap IMA         


        
3条回答
  •  情深已故
    2021-01-05 19:13

    use this function

     void saveImage() {
    
        String root = Environment.getExternalStorageDirectory().toString();
        File myDir = new File(root + "/saved_images");
    
        String fname = "Image.jpg";
        File file = new File (myDir, fname);
        if (file.exists ()) file.delete (); 
        try {
               FileOutputStream out = new FileOutputStream(file);
               myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
               out.flush();
               out.close();
    
        } catch (Exception e) {
               e.printStackTrace();
        }
    }
    

    Check this answer will give more details Android saving file to external storage

提交回复
热议问题