Новый NotificationCompat.Builder(Context) - как установитьLargeIcon() из URL

Пытаюсь .setLargeIcon(getBitmapFromUrl(url) и не настраивается.

    public void showSmallNotification(String title, String message, String url, Intent intent) {
    Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    PendingIntent resultPendingIntent =
            PendingIntent.getActivity(mCtx,ID_SMALL_NOTIFICATION,intent,
                    PendingIntent.FLAG_UPDATE_CURRENT
            );
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(mCtx);

    Notification notification = mBuilder.setSmallIcon(R.drawable.ic_logo_small).setTicker(title).setWhen(0)
            .setAutoCancel(true)
            .setContentIntent(resultPendingIntent)
            .setContentTitle(title)
            .setSmallIcon(R.drawable.ic_logo_small)
            .setLargeIcon(getBitmapFromURL(url))
            .setContentText(message)
            .setPriority(NotificationCompat.PRIORITY_HIGH)
            .setSound(defaultSoundUri)
            .build();

    notification.flags |= Notification.FLAG_AUTO_CANCEL;

    NotificationManager notificationManager = (NotificationManager) mCtx.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(ID_SMALL_NOTIFICATION, notification);
}

здесь я конвертирую изображение из URL в растровое изображение

//The method will return Bitmap from an image URL
private Bitmap getBitmapFromURL(String strURL) {
    try {
        URL url = new URL(strURL);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }

В основном, если бы я мог скользить по изображению. Фантастический. в любом случае, небольшая помощь, чтобы мой уведомитель был простым и крутым, как твиттер и инстаграм. Спасибо..

UPDATED: .setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.drawable.ic_logo_small)) works but url doesn't.

но изображение, которое я хочу установить, находится в сети (mysql)

Я думаю, вам следует использовать локальное изображение, а не URL-адрес.

Cường Nguyễn 21.03.2019 05:46
0
1
665
2
Перейти к ответу Данный вопрос помечен как решенный

Ответы 2

Вы должны скачать изображение перед звонком setLargeIcon

Вы можете загрузить изображение с помощью AsyncTask:

public class DownloadImageAndShowNotification extends AsyncTask<String, Void, Bitmap> {

    private Context mContext;
    private String mUrl;

    DownloadImageAndShowNotification(Context context, String url) {
        super();
        this.mContext = context;
        this.mUrl = url;
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        InputStream in;
        try {
            URL url = new URL(mUrl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.connect();
            in = connection.getInputStream();
            return BitmapFactory.decodeStream(in);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Bitmap result) {
        super.onPostExecute(result);
        // Build your notification with the bitmap
    }
}
Ответ принят как подходящий

если вы используете Glide, вы можете скачать так

GlideApp.with(this).asBitmap().skipMemoryCache(true).load(url).into(150, 150).get()

и вы должны установить уведомление после загрузки изображения,

Другие вопросы по теме