[英]Android + Volley: how to get image bitmap using ImageLoader?
I'm trying to get images for list view using Volley Library. 我正在尝试使用Volley Library获取用于列表视图的图像。 I created simple HTTP helper with the following method.
我使用以下方法创建了简单的HTTP帮助程序。
/**
* 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: 因此,我尝试使用在URL上找到的以下代码创建ImageLoader:
http://www.androidhive.info/2014/05/android-working-with-volley-library-1/ 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! 我认为您的
makeImageRequest
方法将始终返回null
因为调用onResponse
或onErrorResponse
侦听器会花费一些时间,但您会立即返回mBitmap
!
If you want to use Volley's ImageLoader
you'd better get image inside your activity or ... not from another class like your HttpHelperClass
. 如果您想使用Volley的
ImageLoader
,则最好在活动中获取图像,或者...不要从诸如HttpHelperClass
其他类中获取图像。
Also AppController
is a class that extends Application
and you should create it yourself . AppController
也是扩展Application
的类, 您应该自己创建它 。 (It's in your AndroidHive link. Section 3. Creating Volley Singleton class ) (它在您的AndroidHive链接中。 第3节。创建Volley Singleton类 )
And also for caching images you should not create a new ImageLoader
everytime because in this way the caching gets meaningless. 另外,对于缓存图像,您不应该每次都创建一个新的
ImageLoader
因为这样缓存就变得毫无意义了。 You should get that from your AppController
class. 您应该从
AppController
类中获得。
Furthermore I suggest you using Picasso because it's way better that Volley
in image loading and a lot easier! 此外,我建议您使用Picasso,因为与
Volley
,它在图像加载方面要更好,并且容易得多! With Picasso
you only need to call the following line to load an image from web into an ImageView
: 使用
Picasso
您只需调用以下行即可将网络上的图像加载到ImageView
:
Picasso.with(context).load(urlString).to(imageView);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.