Exception in Drawable.createFromResourceStream() — HTC ONLY?

扶醉桌前 提交于 2019-11-30 18:37:31

问题


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.


回答1:


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.




回答2:


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




回答3:


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?



来源:https://stackoverflow.com/questions/7747089/exception-in-drawable-createfromresourcestream-htc-only

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!