Created
November 16, 2022 09:54
-
-
Save mebjas/abeceb3ce4a734b7cbb6c659017be8d3 to your computer and use it in GitHub Desktop.
Code snippet for getting fd value from Uri in Android.
This file contains hidden or 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
Context context = getApplicationContext(); | |
ContentResolver contentResolver = context.getContentResolver(); | |
try (AssetFileDescriptor assetFileDescriptor | |
= contentResolver.openAssetFileDescriptor(imageUri, "r")) { | |
ParcelFileDescriptor parcelFileDescriptor = assetFileDescriptor.getParcelFileDescriptor(); | |
int fd = parcelFileDescriptor.getFd(); | |
// TODO: Read file using the fd in native layer. | |
// Important: Native layer shouldn't assume ownership of this fd and close it. | |
parcelFileDescriptor.close(); | |
} catch (IOException ioException) { | |
// TODO: Handle failure scenario. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment