Drawable.createFromResourceStream() 中的异常——仅限 HTC?

发布于 2024-12-09 07:00:14 字数 1107 浏览 0 评论 0原文

我发布了一个 IME(软键盘)应用程序,并且我仅从HTC 手机收到崩溃报告。这是堆栈跟踪:

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)
    ...

这是我对 Drawable.createFromResourceStream() 的调用

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

,其中 contextInputMethodService 的子类,而 stream>FileInputStreamAssetInputStream (我都尝试过)。资源文件是已编译的NinePatchDrawable。我已经确认流不为空。

重复一遍:此错误仅发生在运行不同版本 Android 操作系统的某些 HTC 手机(包括 Evo)上。

有没有人经历过这个和/或知道如何解决它?

预先感谢,

巴里

P.S.奇怪的是,崩溃行 465 不在 BitmapFactory.java 的任何版本中的崩溃方法 BitmapFactory.decodeResourceStream() 内,因此 HTC 必须使用修改后的代码。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青柠芒果 2024-12-16 07:00:14

找到了此问题的解决方案,您可以将对 Drawable.createFromResourceStream 的调用替换为:

// 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);
}

这仅适用于文件。

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.

心房敞 2024-12-16 07:00:14

您可以只使用 Drawable.createFromStream () 而不是 Drawable.createFromResourceStream()

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

沙沙粒小 2024-12-16 07:00:14

您是否尝试过为 Drawable.createFromResourceStream 提供一整套有效参数?
我查看了 Android 代码,您可以安全地传递虚拟 TypedValue 和虚拟 Options 对象,并且仍然保持默认行为。

那么:

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

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

有人可以在 HTC 设备上验证这一点吗?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文