This is the session 2 of Android Wallpaper App tutorial. In last tutorial, we talked about how to use android gridview to show a list of wallpaper thumbnails. In this tutorial, I will show you how to download a image with a progress dialog. In this wallpaper app example, I already get a list of available wallpaper images. When I click any of thumbnail images, it will start to download the real image from internet and show a progress dialog to trace the downloading progress. Here is screenshot about the final result. You can also download the source code for free and run this example on your own android phones or tablets.

This Android Wallpaper App example includes four tutorials:

Add Progress Dialog in Android App

First of all, I will add a progress dialog in the onCreate function. The following code will initialize the dialogue by setting the ProgressDialog title, ProgressDialog message and style. Later, we will show the dialog and set its progress when user clicks on images in gridview. Here is the source code example:

	protected void onCreate(Bundle savedInstanceState) {
		//grid view
		GridView gridView = (GridView) findViewById(;
		gridView.setAdapter(new ImageAdapter(this));
		//progress bar
		downloadProgressDialog = new ProgressDialog(MainActivity.this);
		downloadProgressDialog.setTitle("Downloading Wallpaper");
		downloadProgressDialog.setMessage("Downloading in progress...");

Add Item Click Listener on GridView

In the second step, I will add an item click listener on the gridview which we created in last tutorial. Inside the listener, I will show the progress dialog and start an asynctask to download the real image.

	private OnItemClickListener gridViewImageClickListener = new OnItemClickListener() {

		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			// TODO Auto-generated method stub;
			new DownloadWallpaperTask().execute(imageURLArray[arg2]);

Download Images with AsyncTask

In the third step, I will create an AsyncTask subclass DownloadWallpaperTask, which will handle the downloading image job, and update the download progress. After the download job finishes, I will hide the download progress dialog and show the whole image in a full size in a new Activity. Please keep in mine, the class DownloadWallpaperTask is an inner class. Here is the source code of this class. If you just want to find a piece of code to download a image, you can also check Download Images by AsyncTask.

private class DownloadWallpaperTask extends AsyncTask <String, Integer, String> {
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        String wallpaperURLStr = params[0];
        String localPath = Integer.toString(wallpaperURLStr.hashCode());
        try {
            URL wallpaperURL = new URL(wallpaperURLStr);
            URLConnection connection = wallpaperURL.openConnection();
            //get file length
            int filesize = connection.getContentLength();
            if(filesize < 0) { //comment >
            } else {
            InputStream inputStream = new BufferedInputStream(wallpaperURL.openStream(), 10240);
            String appName = getResources().getString(R.string.app_name);
            OutputStream outputStream = openFileOutput(localPath, Context.MODE_PRIVATE);
            byte buffer[] = new byte[1024];
            int dataSize;
            int loadedSize = 0;
            while ((dataSize = != -1) {
                loadedSize += dataSize;
                outputStream.write(buffer, 0, dataSize);
        } catch (MalformedURLException e) {
        } catch (IOException e) {
            // TODO Auto-generated catch block
        return localPath;

    protected void onProgressUpdate(Integer... progress) {
    protected void onPostExecute(String result) {
        //open preview activity
        Bundle postInfo = new Bundle();
        postInfo.putString("localpath", result);

        if (previewPanelIntent == null) {
            previewPanelIntent = new Intent(MainActivity.this,



Debug Tips

Some people have problems about getting image length from server. When they are using following code to get the image size, getContentLength returns -1 or a negative value.

int filesize = connection.getContentLength();

That is because the server which hosts your images is not setting a “Content-Length” header in the http response message. That’s a server problem. So in my approach, I am setting a number 1000000, which means the default file size is 1M.

download an image with progress dialog

Try Example Android App

You can download and install this android app by click the following link, or scan the following QR code to download and install on your android phone simply. If you feel it is helpful, please support me by clicking the banner inside the app.
Download and Run This Example on Your Android Phone


Get Full Source Code under $3.99

You will get whole source code at $3.99. It is fully functional, and embedded the Google AdMob module by default. With this source code, you can do everything you want:

  • Load your own images;
  • Monetize the app with your own AdMob publish id;
  • Use the source code in your own project;
  • Publish this app with your Google Play account;
Previous PostNext Post


    1. Hello Robert,

      I have finished my tutorial yet. But here is the short answer:

      WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
      try {
      } catch (IOException e) {

      In next tutorial, I will cover this topic.

  1. In this section:
    if (previewPanelIntent == null) {
    previewPanelIntent = new Intent(MainActivity.this,
    Can u explain me what PreviewPanel class and its content?
    Thanks alot!

Leave a Reply

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