I'm trying to get images for list view using Volley Library. I created simple HTTP helper with the following method.
/**
* Processing Image request and gets the image with given URL
*/
public Bitmap makeImageRequest(String url) {
ImageLoader il = new ImageLoader(queue, new BitmapLruCache());
il.get(url, new ImageLoader.ImageListener() {
@Override
public void onResponse(ImageLoader.ImageContainer response, boolean isImmediate) {
mBitmap = processImageResponse(response);
}
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(Constants.Global.ERROR, "Error: " + error.getMessage());
mBitmap = null;
}
});
return mBitmap;
}
But problem is that:
new BitmapLruCache()
Method is not recognized.
So i tried to create ImageLoader using the following code which i found on the URL:
http://www.androidhive.info/2014/05/android-working-with-volley-library-1/
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
But in thos code i cannot find out where to get
AppController
Because the method code is triggered from the custom
public class HttpHelperClass
And called from the activity using the:
// Try to load remote image from URL
Bitmap bm = http.makeImageRequest("http://camranger.com/wp-content/uploads/2014/10/Android-Icon.png");
ImageView iv = (ImageView) findViewById(R.id.imageView);
iv.setImageBitmap(bm);
Is it the right approach how to load images and how can i repair my code to make succcessfull request?
Many thanks for any advice.
I think your makeImageRequest
method will always return null
because it takes some time for the onResponse
or onErrorResponse
listeners to get called but you return the mBitmap
immidately!
If you want to use Volley's ImageLoader
you'd better get image inside your activity or ... not from another class like your HttpHelperClass
.
Also AppController
is a class that extends Application
and you should create it yourself . (It's in your AndroidHive link. Section 3. Creating Volley Singleton class )
And also for caching images you should not create a new ImageLoader
everytime because in this way the caching gets meaningless. You should get that from your AppController
class.
Furthermore I suggest you using Picasso because it's way better that Volley
in image loading and a lot easier! With Picasso
you only need to call the following line to load an image from web into an ImageView
:
Picasso.with(context).load(urlString).to(imageView);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.