Java 加载通知图像的最佳方法?
加载通知映像的最佳方式是什么 这是我目前的方法:正如您所看到的,图像加载是同步的,因此通知可能会延迟。这是一种糟糕的方式Java 加载通知图像的最佳方法?,java,android,push-notification,Java,Android,Push Notification,加载通知映像的最佳方式是什么 这是我目前的方法:正如您所看到的,图像加载是同步的,因此通知可能会延迟。这是一种糟糕的方式 我真的需要一个答案来继续我的项目。记住,如果您正在使用web加载某些内容,您必须等待一段时间。 有几种解决方案: 从web加载图像,如示例中所示并显示 显示存根映像,稍后通过更新通知显示下载的映像 使用一些有助于最高缓存web英寸的工具。例如,皮卡索 使用Picassolib从url加载图像,如 Picasso.with(this.load(imageUri).into(i
我真的需要一个答案来继续我的项目。记住,如果您正在使用web加载某些内容,您必须等待一段时间。 有几种解决方案:
使用
Picasso
lib从url加载图像,如
Picasso.with(this.load(imageUri).into(imageview_id);
您应该首先在没有图像或占位符的情况下通知通知,然后使用异步任务加载位图,或者使用毕加索和目标回调 将用于第一次通知的生成器提供给任务,加载位图时,将其添加到生成器,然后重新通知 如果在完成图像加载之前存在内容更改的风险,请存储一个变量,该变量标识要显示的当前内容,您可以在renotify之前进行检查 您可以遵循google项目提供的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());
}
}
如果不在UiThread中,您可以创建一个Runnable并在Looper中执行它
final Handler uiHandler = new Handler(Looper.getMainLooper());
uiHandler.post(new Runnable() {
@Override
public void run() {
// Call from here
}
});
毕加索之所以更好,仅仅是因为使用了缓存
我强烈建议您调整在通知中设置的每个位图的大小,因为如果您不这样做,它可以很容易地provoc OutOfMemoryException。这就是我一直在寻找的答案。但是,您确定这是使用毕加索的正确方法吗?这不是通过into()方法直接引用imageView的“经典”方法,但在您的情况下,您必须获得加载的位图结果,因为notification manager不允许您访问imageView。使用Target是如何使用毕加索获得异步位图加载结果的。我得到错误
java.lang.IllegalStateException:应该从主线程调用方法。
在这一行.into(new Target(){
是必须的,或者您可以指定一个执行器。我如何修复此错误?堆栈溢出不是编程竞赛。我们要求问题有一个具体的、可验证的答案。询问“最好的”是行不通的。你需要准确地缩小你所说的“最好”的范围。你的要求是什么?你有什么限制?描述你正在寻找的解决方案,然后有人可以提供它。
// 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
}
});
final Handler uiHandler = new Handler(Looper.getMainLooper());
uiHandler.post(new Runnable() {
@Override
public void run() {
// Call from here
}
});