加载通知图片的最佳方法是什么?
这是我目前的方式:您可以看到图像同步加载,因此可以延迟通知。这是糟糕的方式。
public 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;
}
}
Bitmap bitmap = getBitmapFromURL("https://graph.facebook.com/YOUR_USER_ID/picture?type=large");
// CONSTRUCT THE NOTIFICATION DETAILS
builder.setAutoCancel(true);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentTitle("Some Title");
builder.setContentText("Some Content Text");
builder.setLargeIcon(bitmap);
builder.setContentIntent(pendingIntent);
我真的需要一个答案来继续我的项目。
答案 0 :(得分:3)
请记住,如果您使用网络加载某些内容,则必须等待一段时间。 有几种解决方案:
从示例中加载来自网络的图片并显示。
稍后通过更新通知显示存根图像并显示下载的图像
使用一点可以帮助缓存网络英寸。例如,picaso
答案 1 :(得分:2)
您应首先通知您的通知没有图像或占位符,然后使用AsyncTask加载您的位图,或使用Picasso和Target回调。
为您的任务提供您用于第一次通知的构建器,加载位图时,将其添加到构建器,然后重新通知您的通知。
如果在完成图片加载之前存在内容发生变化的风险,请存储一个标识您当前要显示的内容的变量,您可以在重新通知之前进行检查。
您可以按照Google UniversalMusicPlayer项目提供的MediaNotificationManager的例子进行操作。
在你的情况下:
// CONSTRUCT THE NOTIFICATION DETAILS
builder.setAutoCancel(true);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentTitle("Some Title");
builder.setContentText("Some Content Text");
//builder.setLargeIcon(bitmap); // replace this line with place holder drawable from resources
builder.setContentIntent(pendingIntent);
manager.notify(NOTIFICATION_ID, builder.build());
currentLoadImageTask = new LoadImageTask(manager, builder);
currentLoadImageTask.execute("https://graph.facebook.com/YOUR_USER_ID/picture?type=large");
// ...
static class LoadImageTask extends AsyncTask<String, Void, Bitmap> {
final NotificationManager manager;
final NotificationCompat.Builder builder;
public LoadImageTask(final NotificationManager manager, final NotificationCompat.Builder builder) {
this.manager = manager;
this.builder = builder;
}
@Override
protected Bitmap doInBackground(final String... strings) {
if (strings == null || strings.length == 0) {
return null;
}
try {
final URL url = new URL(strings[0]);
final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
final InputStream input = connection.getInputStream();
return BitmapFactory.decodeStream(input);
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(final Bitmap bitmap) {
if (bitmap == null || manager == null || builder == null) {
return;
}
builder.setLargeIcon(bitmap);
manager.notify(NOTIFICATION_ID, builder.build());
}
}
毕加索:
// CONSTRUCT THE NOTIFICATION DETAILS
builder.setAutoCancel(true);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentTitle("Some Title");
builder.setContentText("Some Content Text");
//builder.setLargeIcon(bitmap); // replace this line with place holder drawable from resources
builder.setContentIntent(pendingIntent);
manager.notify(NOTIFICATION_ID, builder.build());
// ...
Picasso.with(context)
.load("https://graph.facebook.com/YOUR_USER_ID/picture?type=large")
.resize(250, 250)
.into(new Target() {
@Override
public void onBitmapLoaded(final Bitmap bitmap, final Picasso.LoadedFrom from) {
builder.setLargeIcon(bitmap);
manager.notify(NOTIFICATION_ID, builder.build());
}
@Override
public void onBitmapFailed(final Drawable errorDrawable) {
// Do nothing
}
@Override
public void onPrepareLoad(final Drawable placeHolderDrawable) {
// Do nothing
}
});
如果不在UiThread中,您可以创建一个Runnable并在Looper中执行它
final Handler uiHandler = new Handler(Looper.getMainLooper())
uiHandler.post(new Runnable() {
@Override
public void run() {
// Call from here
}
});
Picasso因为使用缓存而更好。
我强烈建议你调整你在通知中设置的每个位图,因为如果你不这样做,它很容易引起OutOfMemoryException。
答案 2 :(得分:0)
使用Picasso
lib从url加载图片,如
Picasso.with(this.load(imageUri).into(imageview_id);