Created
November 14, 2017 04:11
-
-
Save nhancv/a44cc2b4d6a543761b447298ec7e37c8 to your computer and use it in GitHub Desktop.
saveBitmapToFile
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static File saveBitmapToFile(Context context, String filename, Bitmap bitmap) { | |
File f = new File(context.getCacheDir(), filename); | |
Log.e(TAG, "saveBitmapToFile: " + f.getAbsolutePath()); | |
try { | |
f.createNewFile(); | |
//Convert bitmap to byte array | |
ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
bitmap.compress(Bitmap.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(); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
return f; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment