Skip to content

Instantly share code, notes, and snippets.

@Binary-Finery
Created September 8, 2018 20:05
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 Binary-Finery/037e1664d7af494df8baa1a244ed049f to your computer and use it in GitHub Desktop.
Save Binary-Finery/037e1664d7af494df8baa1a244ed049f to your computer and use it in GitHub Desktop.
Save bitmap to external storage and use media scanner to display image in gallery
private void savePalette(Bitmap bitmap) {
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/palette.io");
if(!myDir.exists()){
myDir.mkdirs();
}
String fileName = "palette-io-generated-" + System.currentTimeMillis() + ".jpg";
File file = new File(myDir, fileName);
try {
FileOutputStream out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
MediaScannerConnection.scanFile(this,
new String[]{file.toString()}, null,
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
Msg.build(this, "palette saved").show();
} catch (Exception e) {
e.printStackTrace();
Msg.build(this, e.getMessage()).show();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment