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

Get Bitmap from universal image loader library

我正在尝试从给定的 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);

然后尝试获取 URL 提供的图像:

   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

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

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

试试这个:

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

public class 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;
}

}

public class 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");


        }
    });

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

}

}