简体   繁体   English

Android + Volley:如何使用ImageLoader获取图像位图?

[英]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因为调用onResponseonErrorResponse侦听器会花费一些时间,但您会立即返回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.

相关问题 如何获取使用Volley ImageLoader下载的缓存位图? - How to I get the cached Bitmap I downloaded using Volley ImageLoader? java Android - volley库 - 通过ImageLoader获取一个Bitmap - java Android - volley library - get a Bitmap via ImageLoader 如何在android中使用imageloader释放位图的内存? - How to release memory of bitmap using imageloader in android? 在ImageLoader中加载URL后,如何获取原始位图大小? Android的 - How to get Original Bitmap size after loading URL in ImageLoader | Android 如何使用android volley库将位图图像上传到服务器? - How to upload a bitmap image to server using android volley library? Android Volley ImageLoader-BitmapLruCache参数? - Android Volley ImageLoader - BitmapLruCache parameter? 调用volley ImageLoader方法时如何定义所需图像的尺寸 - How to define the dimension of required Image while calling volley ImageLoader method 在Android中使用canvas和bitmap,如何获取此图像? - Using canvas and bitmap in Android , how to get this image? Android Volley ImageLoader - 如何使用基本HTTP授权? - Android Volley ImageLoader - How to use Basic HTTP Authorization? 使用具有位图的Universal ImageLoader显示图像 - Display Image with Universal ImageLoader with Bitmap
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM