Skip to content

Instantly share code, notes, and snippets.

@bsodhi
Forked from r0xsh/read.java
Created March 6, 2022 04:02
Show Gist options
  • Save bsodhi/75021aaef862a2d9469e32423ec44998 to your computer and use it in GitHub Desktop.
Save bsodhi/75021aaef862a2d9469e32423ec44998 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