I have an image in drawable. I just need to convert this image into a file. How can i achieve this?
            Asked
            
        
        
            Active
            
        
            Viewed 1.0k times
        
    0
            
            
        - 
                    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 Answers
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
- 
                    Thanks for the consideration.I don't want to save the file. I just need to convert the bitmap into file. Is it possible? – Cijo - iRoid Sep 22 '16 at 05:15
- 
                    yeah it is possible, in the above method it is returning the file by converting bitmap. – Nitin Singh Sep 22 '16 at 07:26
- 
                    
