从服务中位图的意图引起的RuntimeException接收广播意图 [英] Bitmap from service to intent causing RuntimeException receiving broadcast intent

查看:267
本文介绍了从服务中位图的意图引起的RuntimeException接收广播意图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我传递一个绘制位图然后把它变成一个字节组,然后再返回。然而,这会导致的onReceive错误。什么我做错了,我怎么可能正确地传递它没有打破接收器?

错误(全在这里: http://pastebin.com/DtBWK8bc ):

 了java.lang.RuntimeException:错误接收广播意向{
ACT = com.mytest.accessibility.CATCH_NOTIFICATION FLG = 0×10(有临时演员)}中
nexter.lpvlock.material.com.accessb.ToastOrNotificationTestActivity$1@b10a35a8

服务:

  =绘制remotePackageContext.getResources()getDrawable(notification.icon)。
        位图米康= convertToBitmap(可拉伸,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
        ByteArrayOutputStream BAOS =新ByteArrayOutputStream();
        mIcon.com preSS(Bitmap.Com pressFormat.PNG,100,BAOS);
        字节[] B = baos.toByteArray();        意图mIntent =新意图(Constants.ACTION_CATCH_NOTIFICATION);
        mIntent.putExtra(文字,notification.tickerText.toString());
        mIntent.putExtra(图标,B);
        。MyAccessibilityService.this.getApplicationContext()sendBroadcast(mIntent);

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

 字节[] B = getIntent()getByteArrayExtra(图标)。
        BMP位图= BitmapFactory.de codeByteArray的(B,0,b.length个);
        iView.setImageBitmap(BMP);


解决方案

我终于得到它的工作!大部分功劳归于用户@MartinPelant(参考:<一href=\"http://stackoverflow.com/a/16258120/2423004\">http://stackoverflow.com/a/16258120/2423004).

我不知道为什么这个错误并弹出,但我假设有一个与位图或ByteArray的问题。我要去承担位图不包含正确的位图/绘制。但在我使用我一直在寻找使用AccessibilityService收到的通知的通知图标。如果你来到这里寻找相同的答案,然后在这里。它可以是(正确)通过在通知中寻找第一个ImageView的检索:

  @覆盖
公共无效onAccessibilityEvent(AccessibilityEvent事件){
    Log.i(onAccessibilityEvent,);
    如果(event.getEventType()== AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED){
        如果(event.getParcelableData()的instanceof通知){
            通知通知=(通知)event.getParcelableData();              尝试{
                extractImage(notification.tickerView);            }赶上(例外五){
                e.printStackTrace();
                extractImage(notification.contentView);
            }
    }
}

用于提取通知的方法,可以在称为链路中找到。

因为有字面上检索什么,但来自AccessibilityService文本或包名没有像样的解释,我张贴这是一个答案。最远的人会得到不这是经过意图绘制/位图,其中最终将导致对上述问题的错误。

修改并只所以它更加清晰,有这个extractImage方法内将它传递给你的活动:

 位图米康= convertToBitmap(绘制,drawable.getIntrinsicWidth(),drawable.getIntrinsicHeight());
    ByteArrayOutputStream BAOS =新ByteArrayOutputStream();
    mIcon.com preSS(Bitmap.Com pressFormat.PNG,100,BAOS);
    字节[] B = baos.toByteArray();    意图mIntent =新意图(Constants.ACTION_CATCH_NOTIFICATION);
    mIntent.putExtra(图标,B);
    。MyAccessibilityService.this.getApplicationContext()sendBroadcast(mIntent);

I'm passing a drawable to bitmap which then gets turned into a bytearray and back again. This however causes an error with onReceive. What am I doing wrong and how may I pass it correctly without breaking the receiver?

Error (full here: 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

Service:

        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 (error points to the BitmapFactory line to be the culprit):

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

解决方案

I finally got it to work! Most of the credit goes to the user @MartinPelant (ref: http://stackoverflow.com/a/16258120/2423004).

I do not know why that error did pop up, but I'm assuming there was an issue with the bitmap or bytearray. I'm going to assume the bitmap did not contain a proper bitmap/drawable. But in my usage I was looking for the notification icon of incoming notifications using AccessibilityService. If you came here looking for the same answer then here. It can be (properly) retrieved by looking for the first ImageView within a notification:

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

The method for extracting the notification can be found at the referred link.

I'm posting this as an answer as there are literally no decent explanation of retrieving anything but text or package name from AccessibilityService. The furthest one will get without this is passing a drawable/bitmap through intent which in the end will lead to the above question's error.

EDIT And just so it's even clearer, have this within the extractImage method to pass it to your activity:

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

这篇关于从服务中位图的意图引起的RuntimeException接收广播意图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆