如何在android通知中设置本地映像

如何在android通知中设置本地映像,android,android-layout,android-activity,Android,Android Layout,Android Activity,我想用存储在SD卡中的用户图像显示通知,我正在获取该图像的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 displayNotificationMes

我想用存储在SD卡中的用户图像显示通知,我正在获取该图像的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");
    }

尝试像这样创建通知,它可能会工作

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

您好,您可以使用以下代码:

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" />
使用以下命令:NotificationCompat.Builder mBuilder=new NotificationCompat.Builderthis.setLargeIconbitmap