简体   繁体   中英

Android Java IOException read failed EBADF (Bad File Number) From File descriptor/Input Stream

All,

I am getting the following error when trying to create a bitmap from a file descriptor running on Android (v18):

java.io.IOException: read failed: EBADF (Bad file number)
at libcore.io.IoBridge.read(IoBridge.java:435)
at java.io.FileInputStream.read(FileInputStream.java:179)
at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:168)
at java.io.BufferedInputStream.read(BufferedInputStream.java:309)
at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:530)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:603)

It is flagging the decode process where it starts. My code is as follows

        MyNativeCallClassClient client = new MyNativeCallClassClient();
        final int fileDescriptor = client.NativeMethodCallGetFD(getShared_memory_name(), getShared_memory_size());
        try {
           Log.d(TAG, "file descriptor number: " + fileDescriptor);
           ParcelFileDescriptor fd = ParcelFileDescriptor.fromFd(fileDescriptor);
           FileDescriptor realFD = fd.getFileDescriptor();
           Bitmap aBitMap= BitmapFactory.decodeFileDescriptor(realFD);
           Log.d(TAG, "BitMap Generated: byte count: " + aBitMap.getByteCount() + ", height: " + aBitMap.getHeight() + ", width: " + aBitMap.getWidth());

        }
        catch (IOException ex) {
           Log.e(TAG, "Issue with decoding file descriptor", ex);
        }

NOTE: I have native calls that give me the file descriptor as an int that is why I have to do the translation there (and I do get a valid number)... Anyway, I check all variables used and they are not null up to the point of 'decodeFileDescriptor(...)'. I also made sure I had the following permissions in Android:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

Also, I even tried putting a synchronize block around the code listed above to make sure there wasn't a threading issue (shouldn't be, but done just in case). Any thoughts? I hope I gave enough information. Thank you.

So, I figured out the issue. I was missing a prefix on the file descriptor. I'm sorry if this doesn't help anyone out. If someone does happen to come across this and has a solution to a similar situation above then please post. Thanks.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM