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