Issue
I'm trying to read a bitmap from the Android /assets
folder using the following code:
AssetFileDescriptor fd = getAssets().openFd("pic1.jpg")
Bitmap bitmap = BitmapFactory.decodeFileDescriptor(fd.getFileDescriptor());
The exception I'm getting suggests that the file permissions are wrong:
java.io.IOException: read failed: EBADF (Bad file number)
at libcore.io.IoBridge.read(IoBridge.java:432)
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:587)
at android.graphics.BitmapFactory.decodeFileDescriptor(BitmapFactory.java:670)
at android.graphics.BitmapFactory.decodeFileDescriptor(BitmapFactory.java:688)
Attempting to read it from the input stream yields a similar exception:
AssetFileDescriptor fd = getAssets().openFd("pic1.jpg");
FileInputStream is = fd.createInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(is);
The exception:
java.io.IOException: lseek failed: EBADF (Bad file number)
Stack trace:
at java.io.FileInputStream.skip(FileInputStream.java:197)
at android.content.res.AssetFileDescriptor$AutoCloseInputStream.<init>(AssetFileDescriptor.java:173)
at android.content.res.AssetFileDescriptor.createInputStream(AssetFileDescriptor.java:138)
Reading directly from the input stream gives yet another exception:
InputStream is = getAssets().open("pic1.jpg");
Bitmap bitmap = BitmapFactory.decodeStream(is);
The exception:
java.lang.NullPointerException: asset
at android.content.res.AssetManager.seekAsset(Native Method)
at android.content.res.AssetManager.access$600(AssetManager.java:35)
at android.content.res.AssetManager$AssetInputStream.mark(AssetManager.java:567)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:572)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:643)
What's the correct way to read assets?
Solution
The problem was that I wasn't reading the asset from the main thread. In the application I was developing, two list items could concurrently loading the same, rather large, asset.
In short, if that file was still being read while another thread attempted to access it, an IOException
would be thrown with "lseek failed: EBADF (Bad file number)".
Answered By - Paul Lammertsma
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.