2015-02-11 3 views
0

я хочу показать уведомление с изображением пользователя, который хранится в моем SDCard, я получаю URL этого образа, то как я могу установить его в качестве largeIcon в уведомленииКак установить локальный образ в андроида уведомления

мой метод, как

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 (растровый)' –

ответ

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(); 
      } 
Смежные вопросы