Sunday, September 13, 2015

Downloading Bulk Images in Android

Hello Android Geeks,

Recently, I got an Android app project in my current work, Wherein the App needs to download bulk images via Webservices/API,
and the requirement of the process of downloading the assets is must be synchronous.

Here's is the sample code that I made to showcase the Android Library of NOSTRA13
entitle "Android-Universal-Image-Loader". for more info about this visit his GITHUB REPO

1.Download and compile the UIL in the Gradle's dependency
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
}


2.Setup your Android Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.sample.xeugene.imagedownload" >

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

    <application
        android:name=".App"
        android:allowBackup="true"
        android:icon="@mipmap/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>


3. MainActivity
package com.sample.xeugene.imagedownload;

import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    final static String TAG = &quot;MAIN_ACTIVITY&quot;;
    ImageLoader imageLoader;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageLoader = ImageLoader.getInstance();

        new DownloadThisImageFromUrl().execute();

    }
    //Running the method imageloader.loadImagesync() must be
    // run in the background thread of the App.
    public class DownloadThisImageFromUrl extends AsyncTask&lt;String, String ,String&gt; {
        @Override
        protected String doInBackground(String... strings) {
            for(String urls : listOfUrls()) {
                Bitmap image = imageLoader.loadImageSync(urls);
                Log.d(TAG, &quot;Download Success: &quot; + image);
                //You can use this var image to save in your SQLite DB :)
            }
            return null;
        }
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Log.d(TAG, &quot;DOWNLOAD COMPLETE!&quot;);
        }
    }

    //Static list url of the Images
    //You can add your desired Images here :)
    public List&lt;String&gt; listOfUrls(){
        List&lt;String&gt; listUrl = new ArrayList&lt;&gt;();
        listUrl.add(&quot;http://dogzone.tcwebsites.netdna-cdn.com/wp-content/uploads/2015/01/Funny-dog-names.jpg&quot;);
        listUrl.add(&quot;http://dreamatico.com/data_images/dog/dog-5.jpg&quot;);
        listUrl.add(&quot;http://static.communitytable.parade.com/wp-content/uploads/2014/02/labrador-america-top-dog-breed-ftr.jpg&quot;);
        return  listUrl;
    }
}


4. Instantiate the Universal Image Loader library in the Application (Singleton)
package com.sample.xeugene.imagedownload;

import android.app.Application;
import android.content.Context;

import com.nostra13.universalimageloader.cache.memory.impl.WeakMemoryCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

/**
 * Created by EugeneAllen on 9/22/2015.
 */
public class App extends Application {


    @Override
    public void onCreate() {
        super.onCreate();

        initImageLoader(getApplicationContext());
    }

    public static void initImageLoader(Context context) {
        DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
                .cacheOnDisc(true).cacheInMemory(true)
                .imageScaleType(ImageScaleType.EXACTLY)
                .displayer(new FadeInBitmapDisplayer(300)).build();

        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                context)
                .defaultDisplayImageOptions(defaultOptions)
                .memoryCache(new WeakMemoryCache())
                .discCacheSize(100 * 1024 * 1024).build();

        ImageLoader.getInstance().init(config);
    }
}

2 comments:

  1. The style of blog presentation is very attractive. This is very interesting post.I am really thankful to you for providing this unique information regarding this sites. Android mobile App development in USA

    ReplyDelete
  2. Hey! This is my first visit to your blog! We are a collection of volunteers and starting a new project in a community in the same niche. Your blog provided us beneficial information to work on. You have done a marvellous job!

    Android Development Company in Texas

    ReplyDelete