Skip to content

Instantly share code, notes, and snippets.

@nhancv
Created November 14, 2017 04:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhancv/a44cc2b4d6a543761b447298ec7e37c8 to your computer and use it in GitHub Desktop.
Save nhancv/a44cc2b4d6a543761b447298ec7e37c8 to your computer and use it in GitHub Desktop.
saveBitmapToFile
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