I am trying to convert a video file into a byte array, like so:
protected void onActivityResult(int requestCode, int resultCode, final @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case VIDEO_SELECT_CODE:
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
try {
FileInputStream importdb = new FileInputStream(getContentResolver().openFileDescriptor(uri, "r").getFileDescriptor());
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int nRead;
byte[] bytes = new byte[100000];
while ((nRead = importdb.read(bytes, 0, bytes.length)) != -1) {
buffer.write(bytes, 0, nRead);
}
}catch(Exception e){
e.printStackTrace();
}
break;
}
}
I get the following error message :
java.io.IOException: read failed: EBADF (Bad file descriptor)
when I try to open some .mp4
files while I don't get it while opening some others.
Why could this be?
(this snippet of code is the onActivityResult
method for the Activity in which I select a video from the phone's internal storage, so uri
is the URI for that selected video file)
You forgot to close the Parcel File Descriptor which is returned by openFileDescriptor()
. Besides Input/Output streams, you must also remember about closing the Asset/Parcel File Descriptor at the end.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.