It’s a common requirement in Android Apps to save images as files in external storage and get a File reference from that saved location. Usually, we have images in the form of Bitmap in Android. In the following article, we will discuss how to Convert Bitmap to File in Android in both Java & Kotlin.
Method to Convert Bitmap to File in Java
public static File bitmapToFile(Context context,Bitmap bitmap, String fileNameToSave) { // File name like "image.png"
//create a file to write bitmap data
File file = null;
try {
file = new File(Environment.getExternalStorageDirectory() + File.separator + fileNameToSave);
file.createNewFile();
//Convert bitmap to byte array
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0 , bos); // YOU can also save it in JPEG
byte[] bitmapdata = bos.toByteArray();
//write the bytes in file
FileOutputStream fos = new FileOutputStream(file);
fos.write(bitmapdata);
fos.flush();
fos.close();
return file;
}catch (Exception e){
e.printStackTrace();
return file; // it will return null
}
}
Method to Convert Bitmap to File in Kotlin
fun bitmapToFile(bitmap: Bitmap, fileNameToSave: String): File? { // File name like "image.png"
//create a file to write bitmap data
var file: File? = null
return try {
file = File(Environment.getExternalStorageDirectory().toString() + File.separator + fileNameToSave)
file.createNewFile()
//Convert bitmap to byte array
val bos = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.PNG, 0, bos) // YOU can also save it in JPEG
val bitmapdata = bos.toByteArray()
//write the bytes in file
val fos = FileOutputStream(file)
fos.write(bitmapdata)
fos.flush()
fos.close()
file
} catch (e: Exception) {
e.printStackTrace()
file // it will return null
}
}
Required Imports
import android.graphics.Bitmap;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import android.os.Environment;
Permission Required to Access External Storage
Most importantly, to save files in external storage we need to add external storage permission in the AndroidManifest file. For that add the following uses-permission tags in your Android Manifest file above the application tag.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
These READ_EXTERNAL_STORAGE & WRITE_EXTERNAL_STORAGE permissions are dangerous permissions which means that to support Android 6.0 (API level 23) or higher, we need to handle it on runtime. For that, I have already written a detailed article to Handle Runtime Permissions in Android.
Explanation
The above methods take 2 parameters. The first parameter is the Bitmap object that you want to save in the file. The second parameter is the fileName by which you want to save the file in your external storage. This method compresses your Bitmap into PNG format and saves it in the External Storage. You can also change its compress format to JPEG. This method will return you the file object that you can use for future reference and retrieving the image again from the file. For that purpose, you can use the following code.
Convert File to Bitmap in Android
Converting File into bitmap just requires one line of code.
Convert File to Bitmap in Java
Bitmap bitmap = BitmapFactory.decodeFile(file.getPath());
Convert File to Bitmap in Kotlin
val bitmap = BitmapFactory.decodeFile(file.getPath())
That’s it. Enjoy 🙂
If you have any question feel free to ask in the following comments section.