Skip to content

Instantly share code, notes, and snippets.

@RikkaW
Created September 30, 2019 08:48
Show Gist options
  • Save RikkaW/db1919b81d28097a03c6ec8948fd3262 to your computer and use it in GitHub Desktop.
Save RikkaW/db1919b81d28097a03c6ec8948fd3262 to your computer and use it in GitHub Desktop.
private boolean insertImage(File image) throws IOException {
ContentValues values;
values = new ContentValues();
values.put(MediaStore.Images.ImageColumns.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + "test");
values.put(MediaStore.Images.ImageColumns.DISPLAY_NAME, Long.toString(System.currentTimeMillis()));
values.put(MediaStore.Images.ImageColumns.IS_PENDING, true);
Uri uri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
if (uri == null) {
return false;
}
InputStream is = new FileInputStream(image);
OutputStream os = getContentResolver().openOutputStream(uri, "rw");
byte[] b = new byte[8192];
for (int r; (r = is.read(b)) != -1; ) {
os.write(b, 0, r);
}
os.flush();
os.close();
is.close();
values = new ContentValues();
values.put(MediaStore.Images.ImageColumns.IS_PENDING, false);
return getContentResolver().update(uri, values, null, null) == 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment