【问题标题】:Bitmap from service to intent causing RuntimeException receiving broadcast intent从服务到意图的位图导致 RuntimeException 接收广播意图
【发布时间】:2014-07-23 17:48:32
【问题描述】:

我将一个可绘制对象传递给位图,然后它会变成一个字节数组并再次返回。然而,这会导致 onReceive 出错。我做错了什么,如何在不损坏接收器的情况下正确传递它?

错误(此处完整:http://pastebin.com/DtBWK8bc):

java.lang.RuntimeException: Error receiving broadcast Intent { 
act=com.mytest.accessibility.CATCH_NOTIFICATION flg=0x10 (has extras) } in 
nexter.lpvlock.material.com.accessb.ToastOrNotificationTestActivity$1@b10a35a8

服务:

        drawable = remotePackageContext.getResources().getDrawable(notification.icon);
        Bitmap mIcon = convertToBitmap(drawable, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        mIcon.compress(Bitmap.CompressFormat.PNG, 100, baos);
        byte[] b = baos.toByteArray();

        Intent mIntent = new Intent(Constants.ACTION_CATCH_NOTIFICATION);
        mIntent.putExtra("text", notification.tickerText.toString());
        mIntent.putExtra("icon", b);
        MyAccessibilityService.this.getApplicationContext().sendBroadcast(mIntent);

Activity(错误指向BitmapFactory行是罪魁祸首):

        byte[] b = getIntent().getByteArrayExtra("icon");
        Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
        iView.setImageBitmap(bmp);

【问题讨论】:

  • 请发布整个堆栈跟踪。
  • @CommonsWare 将 pastebin 添加到问题中。

标签: android android-intent bitmap bytearray


【解决方案1】:

我终于让它工作了!大部分功劳归于用户@MartinPelant(参考:https://stackoverflow.com/a/16258120/2423004)。

我不知道为什么会弹出该错误,但我假设位图或字节数组存在问题。我将假设位图不包含正确的位图/可绘制对象。但在我的使用中,我一直在使用 AccessibilityService 寻找传入通知的通知图标。如果你来这里寻找相同的答案,那么这里。它可以(正确)通过在通知中查找第一个 ImageView 来检索:

 @Override
public void onAccessibilityEvent(AccessibilityEvent event) {
    Log.i("onAccessibilityEvent", "");
    if (event.getEventType() == AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED) {
        if (event.getParcelableData() instanceof Notification) {
            Notification notification = (Notification) event.getParcelableData();

              try {
                extractImage(notification.tickerView);

            } catch (Exception e) {
                e.printStackTrace();
                extractImage(notification.contentView);
            }
    }
}

可以在参考链接中找到提取通知的方法。

我将此作为答案发布,因为除了从 AccessibilityService 中检索文本或包名称之外,几乎没有任何体面的解释。如果没有这个,最远的方法是通过意图传递可绘制/位图,这最终会导致上述问题的错误。

编辑更清楚的是,在 extractImage 方法中将其传递给您的活动:

    Bitmap mIcon = convertToBitmap(drawable, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    mIcon.compress(Bitmap.CompressFormat.PNG, 100, baos);
    byte[] b = baos.toByteArray();

    Intent mIntent = new Intent(Constants.ACTION_CATCH_NOTIFICATION);
    mIntent.putExtra("icon", b);
    MyAccessibilityService.this.getApplicationContext().sendBroadcast(mIntent);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多