Bitmap from service to intent causing RuntimeException receiving broadcast intent

烈酒焚心 提交于 2019-12-25 02:29:46

问题


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

回答1:


I finally got it to work! Most of the credit goes to the user @MartinPelant (ref: https://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);


来源:https://stackoverflow.com/questions/24917433/bitmap-from-service-to-intent-causing-runtimeexception-receiving-broadcast-inten

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