Saturday, October 8, 2011

[android-developers] BitmapFactory.decodeStream() returning null when trying to load image from SD card

I'm currently working on a Live Wallpaper that uses OpenGL and ran
into a problem with decodeStream() always returning null. I've had no
problem when working with images in my assets folder, but I am adding
an option to load an image from the SD card. I get file information
from the gallery and turn that into an absolute path, which I have
verified is correct. Here is the snippet of code which loads the
image:

Texture tex = new Texture();
BitmapFactory.Options opts = new BitmapFactory.Options();
// first lets check the dimensions of the image without loading it
opts.inJustDecodeBounds = true;
BitmapFactory.decodeStream(is, null, opts);

tex.mBmpWidth = opts.outWidth;
tex.mBmpHeight = opts.outHeight;
tex.mTexWidth = Utils.nextPowerOf2(opts.outWidth);
tex.mTexHeight = Utils.nextPowerOf2(opts.outHeight);

Log.i("LWP", "Image size = (" + tex.mBmpWidth + "," + tex.mBmpHeight
+
") Texture size = (" + tex.mTexWidth + "," + tex.mTexHeight +
")");

opts = new BitmapFactory.Options();

try {
tex.mBitmap = BitmapFactory.decodeStream(is);
} catch (OutOfMemoryError e) {
}

Texture is just a static class which has the members shown above. The
first call to decodeStream() where I get the outWidth and outHeigt
works but when I try to decode the image to a bitmap it returns null.

I've tried using a BufferedInputStream and still get the same
results. I've read several entries on SO and here about this
returning null but nothing has offered a remedy to this.

Any Ideas?

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment