This article shows how to download an image and store it in the SD card of your Android phone.
We will be using the Universal Image Loader for displaying a list of images. The user will be able to download the image by clicking on the image in the image list displayed.
Step 1
A small change is required in "listOfImages.java", Start the activity "Download" (created later) to load it when any item of the list is clicked.
- package com.chhavi.downloadimage;
-
- import java.util.Collections;
- import java.util.LinkedList;
- import java.util.List;
- import android.app.Activity;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.AbsListView;
- import android.widget.AdapterView;
- import android.widget.BaseAdapter;
- import android.widget.ImageView;
- import android.widget.ListView;
- import android.widget.TextView;
- import com.nostra13.universalimageloader.core.DisplayImageOptions;
- import com.nostra13.universalimageloader.core.ImageLoader;
- import com.nostra13.universalimageloader.core.assist.ImageLoadingListener;
- import com.nostra13.universalimageloader.core.assist.SimpleImageLoadingListener;
- import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
-
- public class ListOfImages extends Activity {
- protected AbsListView list;
- protected ImageLoader loader = ImageLoader.getInstance();
-
- final Context context=this;
- DisplayImageOptions op;
- String [] images={"http://t2.gstatic.com/images?q=tbn:ANd9GcSZrajzoEXNlRWjMGE9L3kqI1EsFN9P5HCNhMo4xaqLkWuhAixo","http://t0.gstatic.com/images?q=tbn:ANd9GcQH7hisM_szjOKlVdQvq6m_J4lETkWxQOlAk3SMWs051TFFnmWMCA","http://3.bp.blogspot.com/-kAhN0HX-MBk/T_5bApfhbJI/AAAAAAAAAuI/lUww8xT9yV8/s1600/smileys_001_01.png"};
-
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.image_listview_layout);
-
- op = new DisplayImageOptions.Builder()
- .showStubImage(R.drawable.ic_stub)
- .showImageForEmptyUri(R.drawable.ic_empty)
- .cacheInMemory()
- .cacheOnDisc()
- .displayer(new RoundedBitmapDisplayer(20))
- .build();
-
- list = (ListView) findViewById(android.R.id.list);
- ((ListView) list).setAdapter(new ItemAdapter());
- list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
- @Override
- public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
-
- Intent i=new Intent(context,Download.class);
- i.putExtra("pos",position+"");
- startActivity(i);
- }
- });
- }
-
- @Override
- public void onBackPressed() {
- super.onBackPressed();
- }
-
-
- class ItemAdapter extends BaseAdapter {
-
- private class ViewHolder {
- public TextView text;
- public ImageView image;
- }
-
- @Override
- public int getCount() {
- return images.length;
- }
-
- @Override
- public Object getItem(int position) {
- return position;
- }
-
- @Override
- public long getItemId(int position) {
- return position;
- }
-
- @Override
- public View getView(final int position, View convertView, ViewGroup parent) {
- View v = convertView;
- final ViewHolder holder;
- if (convertView == null) {
- v = getLayoutInflater().inflate(R.layout.image_list_layout, parent, false);
- holder = new ViewHolder();
- holder.text = (TextView) v.findViewById(R.id.text);
- holder.image = (ImageView) v.findViewById(R.id.image);
- v.setTag(holder);
- } else {
- holder = (ViewHolder) v.getTag();
- }
-
- holder.text.setText("Image " + (position + 1));
- loader.displayImage(images[position], holder.image, op, null);
-
- return v;
- }
- }
- }
This is the same old code as you saw in the "Universal Image Loader" article. The only change is the starting of the activity "Download" on item click.
Step 2
Make a new layout file and name it as "progress_dialog_layout.xml" and add the following code to it:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/layout_root"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:padding="10dp">
- <TextView
- android:id="@+id/downloadingFrom"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="#FFF"
- android:text="hello"
- android:textStyle="bold"/>
- <TextView
- android:id="@+id/startDownload"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textColor="#b22f2f"
- android:text="hello"
- android:layout_marginTop="5dp"
- android:textStyle="bold|italic" />
- <ProgressBar
- android:id="@+id/progress_bar"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:progress="0"
- android:layout_marginTop="5dp"
- android:layout_marginBottom="10dp"
- style="?android:attr/progressBarStyleHorizontal"
- android:maxHeight="10dip"
- android:minHeight="10dip"
- />
- </LinearLayout>
Step 3
Make a new drawable resource file. Name it "show_progress_slider.xml" and add the following code to it:
- <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:id="@android:id/background">
- <shape>
- <corners android:radius="5dip" />
- <gradient
- android:startColor="#ff9d9e9d"
- android:centerColor="#ff5a5d5a"
- android:centerY="0.75"
- android:endColor="#ff747674"
- android:angle="270" />
- </shape>
- </item>
- <item
- android:id="@android:id/progress">
- <clip>
- <shape>
- <corners
- android:radius="5dip" />
- <gradient
- android:startColor="#ff8e8e"
- android:centerColor="#ff5656"
- android:centerY="0.75"
- android:endColor="#ffa1a1"
- android:angle="270"
- />
- </shape>
- </clip>
- </item>
- </layer-list>
The HttpURLConnection class helps in sending and receiving the data over the web.
"sdCardPath" is the path where the downloaded image will be stored to.
The image file is being transferred by converting it into a File stream.
Step 5
Do the following changes in "AndroidManifest":
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.chhavi.downloadimage"
- android:versionCode="1"
- android:versionName="1.0" >
-
- <uses-permission android:name="android.permission.INTERNET" />
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
-
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="17" />
-
- <application
- android:name=".App"
- 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>
- <activity
- android:name=".ListOfImages"
- android:label="Image list" />
- <activity
- android:name=".Download"
- android:label="Download Image" />
-
- </application>
-
- </manifest>
Output snapshots:
Clicking on "Clik here":
Clicking on any item of the list will download the image to the SD card of the connected phone.
Thank you