Created
September 30, 2019 08:48
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
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