Skip to content

Instantly share code, notes, and snippets.

@pokk
Created January 26, 2017 09:47
Show Gist options
  • Save pokk/3de2774982e9abecb6d9393e6602d382 to your computer and use it in GitHub Desktop.
Save pokk/3de2774982e9abecb6d9393e6602d382 to your computer and use it in GitHub Desktop.
from InputStream to File.

Introduction

We're able to get a Uri from Camera, Gallery, Video Camera Intent. We will obtain a Uri from the intent then you can use it to get a temp File.

// We can get input stream from uri.
InputStream inputStream = getContentResolver().openInputStream(uri);
public static File stream2File(InputStream in) throws IOException {
File tmpFile = File.createTempFile("tmp", ".jpg");
tmpFile.deleteOnExit();
// compile "org.apache.directory.studio:org.apache.commons.io:2.4"
IOUtils.copy(inputStream, new FileOutputStream(tmpFile));
// You got a [tmpFile] from [inputStream].
return tmpFilel;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment