Convert Bitmap to File
Try this:
bitmap.compress(Bitmap.CompressFormat.PNG, quality, outStream);
See this
android, how to convert bitmap to file object
Hope this will help you
private static void persistImage(Bitmap bitmap, String name) {
File filesDir = getAppContext().getFilesDir();
File imageFile = new File(filesDir, name + ".jpg");
OutputStream os;
try {
os = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
os.flush();
os.close();
} catch (Exception e) {
Log.e(getClass().getSimpleName(), "Error writing bitmap", e);
}
}
convert android.graphics.Bitmap to java.io.File
This should do it:
private static void persistImage(Bitmap bitmap, String name) {
File filesDir = getAppContext().getFilesDir();
File imageFile = new File(filesDir, name + ".jpg");
OutputStream os;
try {
os = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
os.flush();
os.close();
} catch (Exception e) {
Log.e(getClass().getSimpleName(), "Error writing bitmap", e);
}
}
Change the Bitmap.CompressFormat
and extension to suit your purpose.
Save Bitmap into File and return File having bitmap image
I try to make some corrections on your code
I assume that you want to use filename instead of bitmap as parameter
private File savebitmap(String filename) {
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
OutputStream outStream = null;
File file = new File(filename + ".png");
if (file.exists()) {
file.delete();
file = new File(extStorageDirectory, filename + ".png");
Log.e("file exist", "" + file + ",Bitmap= " + filename);
}
try {
// make a new bitmap from your file
Bitmap bitmap = BitmapFactory.decodeFile(file.getName());
outStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outStream);
outStream.flush();
outStream.close();
} catch (Exception e) {
e.printStackTrace();
}
Log.e("file", "" + file);
return file;
}
Related Topics
Only Load Layout When Firebase Calls Are Complete
Adding an Onclicklistener to Listview (Android)
Progressdialog Is Deprecated.What Is the Alternate One to Use
How to Set Custom Font for a Whole Application in Android
No Activity Found to Handle Intent:Android.Intent.Action.View
Failed to Find Build Tools Revision 23.0.1
Getting Access to Media Player Cache
Forcing the Soft Keyboard Open
Action_Sendto for Sending an Email
Ndk Resolution Outcome: Project Settings: Gradle Model Version=5.4.1, Ndk Version Is Unknown Error
Google Play Services Out of Date. Requires 3159100 But Found 3158130
What Does the Layoutinflater Attachtoroot Parameter Mean
Android Onclick in Xml VS. Onclicklistener
Set Dynamic Base Url Using Retrofit 2.0 and Dagger 2
Androidx:Appcompat I:Art Error Android.View.View$Onunhandledkeyeventlistener