Convert Bitmap to File

I understand that using BitmapFactory can convert a File to a Bitmap, but is there any way to convert a Bitmap image to a File?

  • I'm trying to search nearby places such as banks, restaurants, ATMs inside the drawn area on google maps in android
  • Translating touch events from Javascript to jQuery
  • How to export an activity so other apps can call it?
  • com.google.android.maps missing?
  • Android Google maps API V2 center markers
  • How to avoid stripping for native code symbols for android app
  • Related posts:

    Customizing the ScrollBar programmatically
    What's the difference between DataOutputStream and ObjectOutputStream?
    Android virtual device manager crashes with “double free or corruption”
    Setting of ImageView's gravity to the center in android programmatically
    Android SDK: Get raw preview camera image without displaying it
    Receive MMS messages in Android KitKat
  • How do you edit a dependency/external library in android studio?
  • Input text dialog Android
  • Implementation of Google design guidelines for Sliders
  • Android SlidingTabs style tabs with round corners
  • How to show Toast in a class extended by BaseAdapter Get View Method
  • Android OpenCV Find Largest Square or Rectangle
  • 4 Solutions collect form web for “Convert Bitmap to File”

    Try this:

    bitmap.compress(Bitmap.CompressFormat.PNG, quality, outStream);
    

    See this

    Hope it will help u:

    //create a file to write bitmap data
    File f = new File(context.getCacheDir(), filename);
    f.createNewFile();
    
    //Convert bitmap to byte array
    Bitmap bitmap = your bitmap;
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
    byte[] bitmapdata = bos.toByteArray();
    
    //write the bytes in file
    FileOutputStream fos = new FileOutputStream(f);
    fos.write(bitmapdata);
    fos.flush();
    fos.close();
    
    File file = new File("path");
    OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
    os.close();
    

    Converting Bitmap to File needs to be done in background (NOT IN THE MAIN THREAD) it hangs the UI specially if the bitmap was large

    File file;
    
    public class fileFromBitmap extends AsyncTask<Void, Integer, String> {
    
        Context context;
        Bitmap bitmap;
        String path_external = Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg";
    
        public fileFromBitmap(Bitmap bitmap, Context context) {
            this.bitmap = bitmap;
            this.context= context;
        }
    
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // before executing doInBackground
            // update your UI
            // exp; make progressbar visible
        }
    
        @Override
        protected String doInBackground(Void... params) {
    
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
            file = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg");
            try {
                FileOutputStream fo = new FileOutputStream(file);
                fo.write(bytes.toByteArray());
                fo.flush();
                fo.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            return null;
        }
    
    
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            // back to main thread after finishing doInBackground
            // update your UI or take action after
            // exp; make progressbar gone
    
             sendFile(file);
    
        }
    }
    

    Calling it

    new fileFromBitmap(my_bitmap, getApplicationContext());
    

    you MUST use the file in onPostExecute .

    To change directory of file to be stored in cache
    replace line :

     file = new File(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg");
    

    with :

    file  = new File(context.getCacheDir(), "temporary_file.jpg");
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.