android - new NotificationCompat.Builder(Context) - 如何从 url setLargeIcon()

标签 android notifications

我正在尝试 .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;
    }

基本上,如果我可以滑动图像。极好的。 不管怎样,一点帮助,这样我的通知程序就会像 Twitter 和 Instagram 一样简单而酷。 谢谢..

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

但是我要设置的镜像是在线的(mysql)

最佳答案

如果你使用Glide,可以这样下载

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

并且必须设置图片下载后的通知,

关于android - new NotificationCompat.Builder(Context) - 如何从 url setLargeIcon(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55272218/

相关文章:

android - 在 Android Studio 布局设计预览中选择目标 API

android - 阻止 startActivity 的默认动画

ios - 我可以在 iOS 8 的通知栏上显示进度条吗

带有大动画 gif 图像的 Android 自定义通知

javascript - Titanium.Network.online 无法在 Android 模拟器上运行

android - 从 App Store 和 Google Play 将每个移动应用程序下载到 Mac 或 PC 的最快/最简单方法是什么?

android - 带有 stax-api jar 的 jackson 依赖构建错误

android - 通知不会在 Android 中播放声音

android - 我可以在 Android 服务中使用共享首选项吗?

android - 是否可以使用delphi读取android上其他应用程序的通知?