Search code examples
androidbitmapsaving-data

Using MediaStore.Images.Media to save bitmap in particular folder


I have to save bitmap drawn on canvas to be saved in my own folder.

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

How should i give a path to the directory? e.g. "/sdcard/MyPictures/"


Solution

  • Use Bitmap.compress to save as JPG or PNG at desired location

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

    Note: 90 is the compression where 100 means no compression. It works for JPG and not for PNG. Don't forget to handle exceptions