<Android> Volley 이미지 불러오기 예제
Android Volley ImageLoader
Volley uses ImageLoader to load images from network, and also to cache them into your Android phone’s in-memory cache by using the LruCache
class. A good approach would be to use Android ImageLoader in a singleton pattern, as same cache should be used throughout the application. Also if used in a singleton pattern your cache would not be activity dependent. Hence whenever you close and open the activity, your images could be picked up from the cache, saving network requests. Now this brings up a question, how to make this volley singleton class? please have a look at the code below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 | package com.truiton.volleyimageloader; import android.content.Context; import android.graphics.Bitmap; import android.util.LruCache; import com.android.volley.Cache; import com.android.volley.Network; import com.android.volley.RequestQueue; import com.android.volley.toolbox.BasicNetwork; import com.android.volley.toolbox.DiskBasedCache; import com.android.volley.toolbox.HurlStack; import com.android.volley.toolbox.ImageLoader; /** * Custom implementation of Volley Request Queue */ 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; } } |
In the above class you may see that a CustomVolleyRequestQueue
is set up which can return an object of ImageLoader
. The class above also creates an ImageCache which works with normal volley cache, but only caching images for requests. Please note here: I just modified the singleton class from my previous Volley Example. Therefore as a whole, the above class can be used to make any type of Android volley requests, be it a JsonRequest, ImageRequest, or just a simple StringRequest.
Android Volley NetworkImageView
A new view introduced with Android volley is NetworkImageView. As a matter of fact this view replaces the standard ImageView, when comes to the usage of volley. Android Volley NetworkImageView is designed in such a way that it creates an image request, sends it and even displays the image, after it is downloaded from the URL. Also it cancels the request on its own if it is detached from the view hierarchy. Hence no need to handle the request cancellations with Volley NetworkImageView. Have a look at the layout for this Android Volley ImageLoader and NetworkImageView Example, it just includes a NetworkImageView instead of ImageView:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#FFFFFF" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <com.android.volley.toolbox.NetworkImageView android:id="@+id/networkImageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:background="#000000"/> </RelativeLayout> |
Next lets have a look at the code for the main activity class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | package com.truiton.volleyimageloader; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import com.android.volley.toolbox.ImageLoader; import com.android.volley.toolbox.NetworkImageView; public class MainActivity extends ActionBarActivity { private NetworkImageView mNetworkImageView; private ImageLoader mImageLoader; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mNetworkImageView = (NetworkImageView) findViewById(R.id .networkImageView); } @Override protected void onStart() { super.onStart(); // Instantiate the RequestQueue. mImageLoader = CustomVolleyRequestQueue.getInstance(this.getApplicationContext()) .getImageLoader(); //Image URL - This can point to any image file supported by Android final String url = "http://goo.gl/0rkaBz"; mImageLoader.get(url, ImageLoader.getImageListener(mNetworkImageView, R.mipmap.truiton_short, android.R.drawable .ic_dialog_alert)); mNetworkImageView.setImageUrl(url, mImageLoader); } } |
As you may see in the above class I used an instance of CustomVolleyRequestQueue
to get the volley ImageLoader object. Further I just specified the URL through setImageUrl
method of NetworkImageView. This handles all the functionality by itself.
Want to display images in a ListView using volley?
If you need to display images in a ListView just use the setImageUrl
method of NetworkImageView in the getView method of your adapter. This will handle all your image requests automatically. Just a reminder, don’t forget to use the NetworkImageView instead of normal ImageView.
Screenshot of Android Volley ImageLoader Example:
출처:http://www.truiton.com/2015/03/android-volley-imageloader-networkimageview-example/
'Android' 카테고리의 다른 글
<안드로이드> html 파싱 - jericho android (2) | 2016.03.25 |
---|---|
안드로이드/Android Custom Dialog 만들기 (0) | 2016.03.24 |
[Android] Camera 호출 후 이미지 Crop하기 예제 (0) | 2016.03.22 |
윈도우 10에서 Android Studio Terminal(터미널) 동작 안할 경우 해결 방법 (0) | 2016.03.21 |
<Android> 우체국 오픈API를 활용한 우편번호 조회 예제 (0) | 2016.03.11 |