android - new NotificationCompat.Builder(Context) - 如何从 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;
}
基本上,如果我可以滑动图像。极好的。无论如何,有点帮助,所以我的通知器将像 twitter 和 instagram 一样简单而酷。谢谢..
UPDATED: .setLargeIcon(BitmapFactory.decodeResource(mCtx.getResources(), R.drawable.ic_logo_small)) works but url doesn't.
但我要设置的图像在线(mysql)
解决方案
您应该在调用之前下载图像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
}
}
推荐阅读
- javascript - How to produce data after getting firebase firestore in Vue.JS
- html - Visual Studio Code 无法识别 HTML 文件
- java - eXist-db: username/password when connecting from Java applications with the XML:DB API
- syntax - 语法错误和逻辑错误
- ios - How to Convert GMUFeature to GMSPath
- machine-learning - fastai conv_learner question导入错误问题
- node.js - 为什么当 Express 服务器发回 304 时,Chrome 控制台会显示 200 响应代码?
- sql - SUM、Count 和 convert -“无法对包含聚合或子查询的表达式执行聚合函数”
- android - 我的 getFilter() 方法有错误吗?
- ios - 通过 Rest API 的 Apple Health Kit 数据