0

I have an image in drawable. I just need to convert this image into a file. How can i achieve this?

Cijo - iRoid
  • 31
  • 1
  • 1
  • 2
  • This question have been asked many times. Please have a look on [this](http://stackoverflow.com/questions/15428975/save-bitmap-into-file-and-return-file-having-bitmap-image). – Developer Sep 22 '16 at 05:15

1 Answers1

1

Try this it works:--

private File saveBitmap(Bitmap bitmap, String path) {
           File file = null;
           if (bitmap != null) {
               file = new File(path);
               try {
                   FileOutputStream outputStream = null;
                   try {
                       outputStream = new FileOutputStream(path); //here is set your file path where you want to save or also here you can set file object directly

                       bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream); // bitmap is your Bitmap instance, if you want to compress it you can compress reduce percentage
                       // PNG is a lossless format, the compression factor (100) is ignored
                   } catch (Exception e) {
                       e.printStackTrace();
                   } finally {
                       try {
                           if (outputStream != null) {
                               outputStream.close();
                           }
                       } catch (IOException e) {
                           e.printStackTrace();
                       }
                   }
               } catch (Exception e) {
                   e.printStackTrace();
               }
           }
           return file;
       }
Nitin Singh
  • 145
  • 6