Exception in Drawable.createFromResourceStream() -- HTC ONLY?
Asked Answered
M

3

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.

Mclendon answered 12/10, 2011 at 21:56 Comment(1)
I am getting the same error. Did you find the solution?Ericaericaceous
E
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.

Ericaericaceous answered 8/2, 2012 at 14:45 Comment(1)
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...Mclendon
S
1

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

Schatz answered 12/1, 2012 at 8:28 Comment(1)
Can't, because he needs the opts.inDensity feature. Drawable.createFromStream () does not support android.graphics.Options.Firkin
F
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?

Firkin answered 14/10, 2013 at 15:8 Comment(3)
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.Mclendon
For the sake of safety, could you try and remember the Android version of the affected devices?Firkin
Unfortunately I cannot remember exactly, but it must have been 2.2 and/or 2.3.Mclendon

© 2022 - 2024 — McMap. All rights reserved.