2015-02-11 47 views
0

我想顯示通知與存儲在我的SD卡用戶的圖像,我得到的是圖像的URL,然後我怎麼可以在通知其設置爲largeIcon如何設置本地圖像在Android通知

我的方法是爲

String userAvatarURL = /storage/emulated/0//Planetskool/Media/Profile Images/ferrari_f12_need_for_speed_rivals-HD%20(1)P47cs5ng7hg4Ft5wquality_50.jpg 


private void displayNotificationMessage(String message) 
    { 
     NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); 

     Log.d("UserAvatarURL", "UserAvatarURL = " + userAvatarURL); 

     mBuilder.setLargeIcon(grabImageFromUrl(userAvatarURL)); 
     mBuilder.setContentTitle(userName); 
     mBuilder.setContentText(message); 

     Intent resultIntent = new Intent(this, MessageThreadActivity.class); 
     TaskStackBuilder stackBuilder = TaskStackBuilder.create(this); 
     stackBuilder.addParentStack(MessageThreadActivity.class); 

// Adds the Intent that starts the Activity to the top of the stack 
     stackBuilder.addNextIntent(resultIntent); 
     PendingIntent resultPendingIntent = 
       stackBuilder.getPendingIntent(
         0, 
         PendingIntent.FLAG_UPDATE_CURRENT 
       ); 
     mBuilder.setContentIntent(resultPendingIntent); 

     NotificationManager mNotificationManager = 
       (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

// notificationID allows you to update the notification later on. 
     mNotificationManager.notify(100, mBuilder.build()); 
    } 

private Drawable grabImageFromUrl(String url) throws Exception { 
     return Drawable.createFromStream((InputStream)new URL(url).getContent(), "src"); 
    } 
+0

使用此:'NotificationCompat.Builder mBuilder =新notificationCompat.Builder(本) .setLargeIcon(位圖)' – 2015-02-11 12:48:36

回答

0

下面嗨,你可以使用代碼:

public static Bitmap getBitmap(String photoPath){ 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inPreferredConfig = Bitmap.Config.ARGB_8888; 
     options.inSampleSize = 16; 
     return BitmapFactory.decodeFile(photoPath, options); 
} 

您還需要添加到這個清單文件

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
0

嘗試建立這樣的通知,它可能工作....

  try { 
       NotificationManager notificationManager = (NotificationManager) ctx 
         .getSystemService(Context.NOTIFICATION_SERVICE); 

       Intent intent = new Intent(ctx, NotificationsActivity.class); 
       intent.putExtra("isFromBadge", false); 


       Notification notification = new Notification.Builder(ctx) 
         .setContentTitle(
           ctx.getResources().getString(R.string.app_name)) 
         .setContentText(message) 
         .setSmallIcon(R.drawable.ic_launcher) 
         .setLargeIcon(bitmap).build(); //bitmap you want to set 

       // hide the notification after its selected 
       notification.flags |= Notification.FLAG_AUTO_CANCEL; 

       notificationManager.notify(1, notification); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
相關問題