Exception in Drawable.createFromResourceStream() -- HTC ONLY?

3.2k Views Asked by At

I've released an IME (soft keyboard) app and I am getting crash reports from HTC phones only. Here is the stack trace:

java.lang.NullPointerException
    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:465)
    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:697)
    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:666)
    at com.comet.android.keyboard.util.Util.getBitmapDrawable(MyFile.java:416)
    ...

Here is my call to Drawable.createFromResourceStream()

drawable = Drawable.createFromResourceStream(context.getResources(), null, stream, null);

where context a subclass of InputMethodService and stream either is a FileInputStream or AssetInputStream (I've tried both). The resource file is a compiled NinePatchDrawable. I've confirmed that stream is not null.

To repeat: this bug only happens with certain HTC handsets (including the Evo) running various versions of Android OS.

Has anyone experienced this and/or know how to fix it?

Thanks in advance,

Barry

P.S. What is strange is that crash line 465 is not within crash method BitmapFactory.decodeResourceStream() in any version of BitmapFactory.java so HTC must be using modified code.

3

There are 3 best solutions below

1
On

You can just use Drawable.createFromStream () instead of Drawable.createFromResourceStream()

3
On

Have you tried supplying Drawable.createFromResourceStream with a full set of valid params? I've looked at the Android code, and you saftely pass both a dummy TypedValue and a dummy Options objects and still maintain the default behaviour.

So:

    Options opts = new BitmapFactory.Options();
    TypedValue dummy = new TypedValue(); 

    Drawable d = Drawable.createFromResourceStream( mContext.getResources(), dummy, in, assetPath, opts);

Can anyone verify this on an HTC device?

1
On

Found a solution for this problem, you can replace the call to Drawable.createFromResourceStream with:

// set options to resize the image
Options opts = new BitmapFactory.Options();
opts.inDensity = 160;

Drawable drawable  = null;
Bitmap bm = BitmapFactory.decodeFile(file.getAbsolutePath(), opts);
if (bm != null) {
  drawable = new BitmapDrawable(context.getResources(), bm);
}

This only works with files.