Skip to content

Instantly share code, notes, and snippets.

@m7mdra
Created April 25, 2018 11:37
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 m7mdra/0461416557b0a999392acdf3a6f2717f to your computer and use it in GitHub Desktop.
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
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