2017-08-10 32 views
0

我有一个应用程序,在该应用程序中,我读取了来自设备的所有传入通知。我在将小图标转换为字节[]时遇到问题。我该怎么做,请帮忙?如何使用NotificationListenerService将小图标从通知转换为byte []?

代码: -

Bundle extras = sbn.getNotification().extras; 
    String title = extras.getString("android.title"); 
    String text = extras.getCharSequence("android.text").toString(); 
    int id1 = extras.getInt(Notification.EXTRA_SMALL_ICON); 
+0

为什么你要转换为字节数组? –

+0

它的应用程序要求 – Niraj

回答

0
//use : 
    Context remotePackageContext = null; 
    Bitmap bmp = null; 
    try { 
     remotePackageContext = getApplicationContext().createPackageContext(pack, 0); 
     Drawable icon = remotePackageContext.getResources().getDrawable(your_id); 
     if(icon !=null) { 
      bmp = ((BitmapDrawable) icon).getBitmap(); 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 


//now convert this bitmap to byte : 

ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
      bmp.compress(Bitmap.CompressFormat.PNG, 90, baos); 
      byte[] b = baos.toByteArray(); 
+0

它显示白色 – Niraj

+0

我想.... remotePackageContext = getApplicationContext()。createPackageContext(pack,0); Drawable icon = ContextCompat.getDrawable(remotePackageContext,id1); – Niraj