3

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.

Barry Fruitman
  • 12,316
  • 13
  • 72
  • 135

3 Answers3

4

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.

peceps
  • 17,370
  • 11
  • 72
  • 79
  • Actually I need a solution that works with files instead of resources. However I'm using 9-patch PNGs which must be pre-compiled so I'm not sure this solution will work for me. I'll try it out and see... – Barry Fruitman Feb 08 '12 at 21:59
1

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

musefan
  • 47,875
  • 21
  • 135
  • 185
sinaeyo
  • 11
  • 1
  • Can't, because he needs the opts.inDensity feature. Drawable.createFromStream () does not support android.graphics.Options. – Vaiden Oct 14 '13 at 14:41
0

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?

Vaiden
  • 15,728
  • 7
  • 61
  • 91
  • I can no longer reproduce this bug -- it is 2 years old -- so I can't test your fix. However I'm pretty sure I tried every variation of Drawable.create*() and they all failed. Ultimately they all reached line 465 of BitmapFactory.java and crashed. – Barry Fruitman Oct 14 '13 at 18:31
  • For the sake of safety, could you try and remember the Android version of the affected devices? – Vaiden Oct 15 '13 at 08:34
  • Unfortunately I cannot remember exactly, but it must have been 2.2 and/or 2.3. – Barry Fruitman Oct 15 '13 at 18:20