Android Java IOException read failed EBADF (Bad File Number) From File descriptor/Input Stream
<p>All,</p>

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

<pre><code>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)
</code></pre>

<p>It is flagging the decode process where it starts. My code is as follows</p>

<pre><code>        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);
        }
</code></pre>

<p>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:</p>

<pre><code> &lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/&gt;
 &lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/&gt;
</code></pre>

<p>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.</p>