network imageview android

network imageview android

I have written more about volley in my previous tutorial Volley Android Example.This is a sample for network imageview android loading images in image view in using volley lib network image view.

Glide – Image loading framework for Android

Android Recyclerview Material Design


Steps to follow:

1.Create a new project.

2. Open build.gradle and add volley support library,

compile 'com.android.volley:volley:1.0.0'
3.Create AppController.java Singleton class where we initialize all the volley core libs.
public class AppController extends Application { 
    public static final String TAG = AppController.class.getSimpleName();
    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;
    private static AppController mInstance;
    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }
    public static synchronized AppController getInstance() {
        return mInstance;
    }
    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }
        return mRequestQueue;
    }
    public ImageLoader getImageLoader() {
        getRequestQueue();
        if (mImageLoader == null) {
            mImageLoader = new ImageLoader(this.mRequestQueue,
                    new LruBitmapCache());
        }
        return this.mImageLoader;
    }   
}

4.add the AppController.java into the Application tag in manifest.xml to execute this at app launch.and also add internet permission.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.volleynetworkimageview"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:name="com.example.volleynetworkimageview.AppController"
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

5.add the NetworkImageView View into the main layout.

<com.android.volley.toolbox.NetworkImageView
        android:id="@+id/networkImageView"
        android:layout_width="fill_parent"
        android:layout_height="130dp"
        android:layout_margin="10dp"
        android:scaleType="fitXY" >
    </com.android.volley.toolbox.NetworkImageView>

6.Add LruBitmapCache.java for cache the image data.

public class LruBitmapCache extends LruCache<String, Bitmap> implements
ImageCache {
public static int getDefaultLruCacheSize() {
final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
final int cacheSize = maxMemory / 8;
return cacheSize;
}
public LruBitmapCache() {
this(getDefaultLruCacheSize());
}
public LruBitmapCache(int sizeInKiloBytes) {
super(sizeInKiloBytes);
}
@Override
protected int sizeOf(String key, Bitmap value) {
return value.getRowBytes() * value.getHeight() / 1024;
}
@Override
public Bitmap getBitmap(String url) {
return get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
put(url, bitmap);
}
}

7. Load the image using network imageview.

imgLoader=AppController.getInstance().getImageLoader();
networkView.setImageUrl(imageUrl, imgLoader);

Another method, Load the image without using network imagview.

imgLoader.get(imageUrl, new ImageListener() {
@Override
public void onErrorResponse(VolleyError error) {
/ TODO Auto-generated method stub 
}
@Override
public void onResponse(ImageContainer response, boolean arg1) {
// TODO Auto-generated method stub
if(response.getBitmap()!=null){
imageView1.setImageBitmap(response.getBitmap());
}
}
});

Another Way, Load the image with the loading indicator,

imgLoader.get(imageUrl, ImageLoader.getImageListener(imageView2, R.drawable.loading_image, R.drawable.loading_image) );

Retrofit Android Example With Recyclerview

Android Recyclerview Material Design


Screenshot

network imageview android

Download

Leave a Reply

Your email address will not be published. Required fields are marked *