Created
April 25, 2018 11:37
-
-
Save m7mdra/0461416557b0a999392acdf3a6f2717f to your computer and use it in GitHub Desktop.
create image file in internal application cache directory and returns the newly created file uri
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 String saveImageByUri(Uri uri) { | |
File parent = new File(getCacheDir(), "temp_images"); | |
try { | |
final InputStream inputStream = getContentResolver().openInputStream(uri); | |
if (inputStream == null) | |
return ""; | |
final BufferedSource source = Okio.buffer(Okio.source(inputStream)); | |
//create file named with same format temp24734194048114480959349.png | |
final File file = File.createTempFile(String.format(Locale.ENGLISH, "temp%d", SystemClock.currentThreadTimeMillis()), ".png", parent); | |
final Sink sink = Okio.sink(file); | |
source.readAll(sink); | |
sink.flush(); | |
sink.close(); | |
return file.getPath(); | |
} catch (java.io.IOException e) { | |
e.printStackTrace(); | |
return ""; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment