位图图像无法在Android通知中显示?

时间:2012-06-22 03:44:00

标签: android

我想将位图图像显示为通知,但是下面的代码在我出错的地方不起作用我无法找到我从位图形式的url获取图像但无法显示。

我在NotiIcon变量中得到位图图像,其位置类型为bitmap。我添加了获取位图图像。

提前致谢。

public static void ShowNotification()
{    
    AsyncTask<Void, Void, Void> t = new AsyncTask<Void, Void, Void>(){  
        protected Void doInBackground(Void... p) {
            String NS =  Context.NOTIFICATION_SERVICE;

            /*  String dtitle = Title;
            String dbody = body; */

            Intent notificationIntent1 =new Intent(appContext, ChromeTestActivity.class);
            PendingIntent contentIntent1 = PendingIntent.getActivity(appContext, 0, notificationIntent1, 0);
            NotificationManager mNm = (NotificationManager)   appContext.getSystemService(NS);
            Notification.Builder builder = new Notification.Builder(appContext);
            builder.setContentIntent(contentIntent1).setLargeIcon(NotiIcon)
                    .setWhen(System.currentTimeMillis())
                    .setAutoCancel(false)
                    .setContentTitle(Title)
                    .setContentText(body);
            Notification n = builder.getNotification();
            mNm.notify(NOTIFICATION_ID, n);
            textTitle.setText(Title);
            textBody.setText(body);
            textTitle.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Log.v(LOGTAG, "Inside NotificationUIManager handleButtonClick");
                }
            });

            textBody.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    Log.v(LOGTAG, "Inside NotificationUIManager handleButtonClick");
                }
            });
            return null;
        }
    };
    t.execute();
}

public static Bitmap DecodeIconUrl(String paramString1)
{
    NotiIcon = null;  
    try
    {
        URL iconurl =  new URL(paramString1);
        URLConnection conn =  iconurl.openConnection();
        conn.connect();
        InputStream is = conn.getInputStream();
        BufferedInputStream bis =  new BufferedInputStream(is);
        NotiIcon = BitmapFactory.decodeStream(bis);
        bis.close();
        is.close();
    }
    catch(IOException e){

    }
return NotiIcon;
}

1 个答案:

答案 0 :(得分:0)

我们必须在这样的通知中添加Icon

检查这个

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

long when = System.currentTimeMillis();
Notification notification = new Notification(R.drawable.notify_icon, "Name", when);

notification.defaults |= Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE;

Intent notificationIntent = new Intent(this, Login.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

notification.setLatestEventInfo(context, "Title", "contentText", contentIntent);
mNotificationManager.notify(ID, notification);