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

时间:2015-02-11 12:41:18

标签: 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 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");
    }

2 个答案:

答案 0 :(得分: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();
            }

答案 1 :(得分: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" />