从通知的Andr​​oid URL加载图像 [英] Load image from url in notification Android

本文介绍了从通知的Andr​​oid URL加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用程序,我想动态设置通知图标将从URL被加载。对于这一点,我已经使用NotificationBuilder的 setLargeIcon 属性接收。我下文称多联系,但并尝试不同的解决方案,但couldn T获得所需的输出。虽然我是从网上下载的URL和设置在通知位图图像,它没有显示,而是显示 setSmallIcon 图像作为大图标。我不知道我要去的地方错了。在这里,我张贴我的code。请帮我解决这个问题。谢谢你。

code:

  @燮pressLint(NewApi)
公共类C2DMMessageReceiver扩展广播接收器{    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){
        字符串行动= intent.getAction();
        如果(com.google.android.c2dm.intent.RECEIVE.equals(动作)){
            Log.e(C2DM,收到的消息);
            最后弦乐全名= intent.getStringExtra(信息);
            最后弦乐payload1 = intent.getStringExtra(MESSAGE1);
            最后弦乐payload2 = intent.getStringExtra(消息2);
            最后弦乐userImage = intent.getStringExtra(userImage);            Log.e(userImage URL:,userImage); //它显示正确的URL            新sendNotification时(上下文)
                    .execute(全名,payload1,userImage);
        }
    }私有类sendNotification的扩展的AsyncTask<弦乐,太虚,位图> {        上下文CTX;
        字符串消息;        公共sendNotification时(上下文的背景下){
            超();
            this.ctx =背景;
        }        @覆盖
        保护位图doInBackground(字符串... PARAMS){            在的InputStream;
            消息=参数[0] +参数[1];
            尝试{                。在=新的URL(PARAMS [2])的OpenStream();
                BMP位图= BitmapFactory.de codeStream(中);
                返回BMP;            }赶上(MalformedURLException的E){
                e.printStackTrace();
            }赶上(IOException异常五){
                e.printStackTrace();
            }
            返回null;
        }        @覆盖
        保护无效onPostExecute(位图结果){            super.onPostExecute(结果);
            尝试{
                NotificationManager notificationManager =(NotificationManager)CTX
                        .getSystemService(Context.NOTIFICATION_SERVICE);                意向意图=新意图(CTX,NotificationsActivity.class);
                intent.putExtra(isFromBadge,FALSE);
                通知通知=新Notification.Builder(CTX)
                        .setContentTitle(
                                ctx.getResources()的getString(R.string.app_name))
                        .setContentText(消息)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setLargeIcon(结果).build();                //隐藏其选中后通知
                notification.flags | = Notification.FLAG_AUTO_CANCEL;                notificationManager.notify(1,通知);            }赶上(例外五){
                e.printStackTrace();
            }
        }
    }


解决方案

改变了我的code下面和现在的工作:

 私有类sendNotification的扩展的AsyncTask<弦乐,太虚,位图> {        上下文CTX;
        字符串消息;        公共sendNotification时(上下文的背景下){
            超();
            this.ctx =背景;
        }        @覆盖
        保护位图doInBackground(字符串... PARAMS){            在的InputStream;
            消息=参数[0] +参数[1];
            尝试{ 网址URL =新的URL(PARAMS [2]);
        HttpURLConnection的连接=(HttpURLConnection类)url.openConnection();
        connection.setDoInput(真);
        connection.connect();
        在= connection.getInputStream();
        位图MYBITMAP = BitmapFactory.de codeStream(中);
        返回MYBITMAP;
            }赶上(MalformedURLException的E){
                e.printStackTrace();
            }赶上(IOException异常五){
                e.printStackTrace();
            }
            返回null;
        }        @覆盖
        保护无效onPostExecute(位图结果){            super.onPostExecute(结果);
            尝试{
                NotificationManager notificationManager =(NotificationManager)CTX
                        .getSystemService(Context.NOTIFICATION_SERVICE);                意向意图=新意图(CTX,NotificationsActivity.class);
                intent.putExtra(isFromBadge,FALSE);
                通知通知=新Notification.Builder(CTX)
                        .setContentTitle(
                                ctx.getResources()的getString(R.string.app_name))
                        .setContentText(消息)
                        .setSmallIcon(R.drawable.ic_launcher)
                        .setLargeIcon(结果).build();                //隐藏其选中后通知
                notification.flags | = Notification.FLAG_AUTO_CANCEL;                notificationManager.notify(1,通知);            }赶上(例外五){
                e.printStackTrace();
            }
        }
    }

In my android application, i want to set Notification icons dynamically which will be loaded from URL. For that, i have used setLargeIcon property of NotificationBuilder in receiver.I reffered many link but and tried various solutions but couldn't get desired output. Though i am downloaded that image from url and setting that bitmap in notification, its not being displayed,instead it displays the setSmallIcon image as large icon. I don't know where i am going wrong. Here i am posting my code. Please help me to solve this issue. Thank you.

Code:

@SuppressLint("NewApi")
public class C2DMMessageReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) {
            Log.e("C2DM", "received message");
            final String fullName = intent.getStringExtra("message");
            final String payload1 = intent.getStringExtra("message1");
            final String payload2 = intent.getStringExtra("message2");
            final String userImage = intent.getStringExtra("userImage");

            Log.e("userImage Url :", userImage); //it shows correct url

            new sendNotification(context)
                    .execute(fullName, payload1, userImage);
        }
    }

private class sendNotification extends AsyncTask<String, Void, Bitmap> {

        Context ctx;
        String message;

        public sendNotification(Context context) {
            super();
            this.ctx = context;
        }

        @Override
        protected Bitmap doInBackground(String... params) {

            InputStream in;
            message = params[0] + params[1];
            try {

                in = new URL(params[2]).openStream();
                Bitmap bmp = BitmapFactory.decodeStream(in);
                return bmp;

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Bitmap result) {

            super.onPostExecute(result);
            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(result).build();

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

                notificationManager.notify(1, notification);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

解决方案

Changed my code as below and its working now :

private class sendNotification extends AsyncTask<String, Void, Bitmap> {

        Context ctx;
        String message;

        public sendNotification(Context context) {
            super();
            this.ctx = context;
        }

        @Override
        protected Bitmap doInBackground(String... params) {

            InputStream in;
            message = params[0] + params[1];
            try {

 URL url = new URL(params[2]);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        in = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(in);
        return myBitmap;




            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Bitmap result) {

            super.onPostExecute(result);
            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(result).build();

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

                notificationManager.notify(1, notification);

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

这篇关于从通知的Andr​​oid URL加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆