如何在服务中使用Universal-Image-Loader?

时间:2014-11-03 18:03:33

标签: android android-service universal-image-loader illegalstateexception

我正在制作图片通知。

我使用GCM推送。通过GCM,我发送了一个图像URI。

当Android应用程序中的服务获取URI时,通过Universal-Image-Loader,我尝试在URI处加载图像。

但在这种情况下,我收到了一个错误:

java.lang.IllegalStateException: ImageLoader.displayImage(...) must be invoked from the main thread or from Looper thread

这是我在下面的服务中的代码:

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getApplicationContext()).build();
    ImageLoader imageloader = ImageLoader.getInstance();
    //ImageLoader.getInstance().destroy();
    imageloader.init(config);

    imageloader.loadImage(img_url, new SimpleImageLoadingListener() {

        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
            notificationWithBigPicture(GcmIntentService.this, PUSH_TITLE, PUSH_CONTENT, R.drawable.ic_stat_gcm, loadedImage, GcmMain.class);
        }
    }); 


public void notificationWithBigPicture(Context context, String title, String message, int icon, Bitmap banner, Class<?> activityClass) {
    Log.i(TAG,"6666");

    Intent intent = new Intent(context, activityClass);
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
    .setSmallIcon(icon)
    .setTicker(title)
    .setContentTitle(title)
    .setContentText(message)
    .setAutoCancel(true);

    NotificationCompat.BigPictureStyle style = new NotificationCompat.BigPictureStyle();
    //style.setBigContentTitle(title);
    //style.setSummaryText(message);
    style.bigPicture(banner);

    builder.setStyle(style);
    builder.setContentIntent(pendingIntent);

    builder.setDefaults(Notification.DEFAULT_VIBRATE);
    builder.setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
    NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID, builder.build());
    Log.v("9999","");
}

但是当代码流进入方法public void onLoading Complete()时, 应用程序已消失并显示错误。

有什么问题?

1 个答案:

答案 0 :(得分:1)

在版本1.9.1之前,需要在主线程中调用loadImage()方法。 从版本1.9.2及更高版本开始,此要求已过期。你可以在任何地方调用loadImage()。

相关问题