从通用图像加载器库获取位图

时间:2015-01-21 11:38:35

标签: android bitmap universal-image-loader

我正在尝试从给定的URL获取位图,因为我正在使用UIL库。由于某种原因,我无法获取位图。这是我正在尝试的。

初始化变量:

  private ImageLoader imageLoader;
  private ImageLoaderConfiguration config;

然后使用imageloader初始化配置:

    config = new ImageLoaderConfiguration.Builder(this)
    .threadPriority(Thread.NORM_PRIORITY - 2)
    .denyCacheImageMultipleSizesInMemory()
    .diskCacheFileNameGenerator(new Md5FileNameGenerator())
    .diskCacheSize(50 * 1024 * 1024) // 50 Mb
    .tasksProcessingOrder(QueueProcessingType.LIFO)
    .writeDebugLogs() // Remove for release app
    .build();

    ImageLoader.getInstance().init(config);

然后尝试获取提供的图片网址:

   Bitmap bmp = imageLoader.loadImageSync(ImageUrl); 

将bmp数据存储到数组中以供进一步使用。

我收到以下错误:

Attempt to invoke virtual method 'android.graphics.Bitmap com.nostra13.universalimageloader.core.ImageLoader.loadImageSync(java.lang.String)' on a null object reference

我不确定这里有什么问题?有人可以帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:1)

您的imageLoader为空,因为您没有设置它:

试试这个:

imageLoader =  ImageLoader.getInstance();
imageLoader.init(config);

答案 1 :(得分:0)

公共类CustomVolleyRequestQueue {

private static CustomVolleyRequestQueue mInstance;
private static Context mCtx;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;


private CustomVolleyRequestQueue(Context context) {
    mCtx = context;
    mRequestQueue = getRequestQueue();

    mImageLoader = new ImageLoader(mRequestQueue,
            new ImageLoader.ImageCache() {
                private final LruCache<String, Bitmap>
                        cache = new LruCache<String, Bitmap>(20);

                @Override
                public Bitmap getBitmap(String url) {
                    return cache.get(url);
                }

                @Override
                public void putBitmap(String url, Bitmap bitmap) {
                    cache.put(url, bitmap);
                }
            });
}

public static synchronized CustomVolleyRequestQueue getInstance(Context context) {
    if (mInstance == null) {
        mInstance = new CustomVolleyRequestQueue(context);
    }
    return mInstance;
}

public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        Cache cache = new DiskBasedCache(mCtx.getCacheDir(), 10 * 1024 * 1024);
        Network network = new BasicNetwork(new HurlStack());
        mRequestQueue = new RequestQueue(cache, network);
        // Don't forget to start the volley request queue
        mRequestQueue.start();
    }
    return mRequestQueue;
}

public ImageLoader getImageLoader() {
    return mImageLoader;
}

}

公共类CreateNotificationActivity扩展了AppCompatActivity {

private NetworkImageView mNetworkImageView;
private ImageLoader mImageLoader;
Bitmap bit;

String url = "http://www.whitegadget.com/attachments/pc-wallpapers/145032d1392006652-nature-wallpaper-nature-picture.jpg";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create_notification);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mNetworkImageView = (NetworkImageView) findViewById(R.id.networkImageView1);

    //bit = BitmapFactory.decodeResource(getResources(), R.drawable.bgd);

    //Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bgd);

    mImageLoader = CustomVolleyRequestQueue.getInstance(this.getApplicationContext())
            .getImageLoader();

    Bitmap bitmap =mImageLoader.get(url, ImageLoader.getImageListener(mNetworkImageView,
            R.mipmap.ic_launcher, R.drawable.icon)).getBitmap();



    Button btn = (Button) findViewById(R.id.button1);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            createNotification();
        }
    });


}

@Override
protected void onStart() {
    super.onStart();

    mImageLoader = CustomVolleyRequestQueue.getInstance(this.getApplicationContext())
            .getImageLoader();

    bit =mImageLoader.get(url, ImageLoader.getImageListener(mNetworkImageView,
            R.mipmap.ic_launcher, R.drawable.icon)).getBitmap();


}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void createNotification()
{



    bit = BitmapFactory.decodeResource(getResources(), R.drawable.bgd);

    mImageLoader.get(url, new ImageLoader.ImageListener() {
        @Override
        public void onResponse(ImageLoader.ImageContainer imageContainer, boolean b) {
            bit =mImageLoader.get(url, ImageLoader.getImageListener(mNetworkImageView,
                    R.mipmap.ic_launcher, android.R.drawable
                            .ic_dialog_alert)).getBitmap();

            Intent intent = new Intent(getApplicationContext(),NotificationReceiverActivity.class);
            PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), (int) System.currentTimeMillis(), intent, 0);


            Notification noti = new Notification.Builder(getApplicationContext())
                    .setContentTitle("Mail Notification")
                    .setContentText("5 new unread messages")
                    .setSmallIcon(R.drawable.bgd)
                    .setStyle(new Notification.BigPictureStyle().bigPicture(bit))
                    .setAutoCancel(true)
                    .setContentIntent(pIntent).build();


            NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

            noti.flags |= Notification.FLAG_AUTO_CANCEL;

            notificationManager.notify(0,noti);

            Log.i("LOG>>>>>>>>>>>>>>>>>>>","inside the on response");
        }

        @Override
        public void onErrorResponse(VolleyError volleyError) {
            bit = BitmapFactory.decodeResource(getResources(), R.drawable.bgd);
            Log.i("LOG>>>>>>>>>>>>>>>>>>>","inside the errror response");


        }
    });

// bit = mImageLoader.get(url,ImageLoader.getImageListener(mNetworkImageView, // R.mipmap.ic_launcher,android.R.drawable // .ic_dialog_alert))。getBitmap();

}

}