Skip to content

Instantly share code, notes, and snippets.

@r0xsh
Created April 12, 2019 07:46
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save r0xsh/e617021b63033a2a4939a9e15879af1d to your computer and use it in GitHub Desktop.
Save r0xsh/e617021b63033a2a4939a9e15879af1d to your computer and use it in GitHub Desktop.
Android: Read file contents from Uri
public static byte[] readUri(Context context, Uri uri) throws IOException {
ParcelFileDescriptor pdf = context.getContentResolver().openFileDescriptor(uri, "r");
assert pdf != null;
assert pdf.getStatSize() <= Integer.MAX_VALUE;
byte[] data = new byte[(int) pdf.getStatSize()];
FileDescriptor fd = pdf.getFileDescriptor();
FileInputStream fileStream = new FileInputStream(fd);
fileStream.read(data);
return data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment