Skip to content

WebNinjaDeveloper.com

Programming Tutorials




Menu
  • Home
  • Youtube Channel
  • Official Blog
  • Nearby Places Finder
  • Direction Route Finder
  • Distance & Time Calculator
Menu

Android Java Project to Download Multiple Images From URL With Progressbar & Save it inside Gallery

Posted on March 2, 2023

 

Welcome folks today in this blog post we will be downloading multiple images from url with progressbar widget and save it inside the gallery. All the full source code of the application is shown below.

 

 

Get Started

 

 

In order to get started you need to make a new android project inside the android studio and then you will see the below directory structure as shown below at the end of the project.

 

 

 

 

 

Now we need to edit the AndroidManifest.xml file you need to include the internet and external storage permission as shown below

 

 

AndroidManifest.xml

 

 

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
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">
 
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
 
 
    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/Theme.CameraProject"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
 
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
 
</manifest>

 

 

Now we need to edit the activity_main.xml layout file where we have the button widget which will say download Images when user will hit that button we will begin downloading multiple images from urls

 

 

activity_main.xml

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?xml version="1.0" encoding="utf-8"?>
<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"
    tools:context=".MainActivity">
 
    <Button
        android:id="@+id/download_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Download Images"
        android:layout_centerInParent="true"/>
 
</RelativeLayout>

 

 

And now we need to edit the MainActivity.java file and copy paste the following code

 

 

MainActivity.java

 

 

Java
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
package com.example.cameraproject;
 
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
 
import androidx.appcompat.app.AppCompatActivity;
 
import java.util.ArrayList;
 
public class MainActivity extends AppCompatActivity {
 
    private Button downloadButton;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        downloadButton = findViewById(R.id.download_button);
        downloadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ArrayList<String> imageUrls = new ArrayList<>();
                imageUrls.add("https://procodestore.com/wp-content/uploads/2021/03/164508084_271381191136191_654097929788476286_n.jpg");
                imageUrls.add("https://images.unsplash.com/photo-1677748717654-2e6630483a15?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxlZGl0b3JpYWwtZmVlZHw1fHx8ZW58MHx8fHw%3D&auto=format&fit=crop&w=500&q=60");
                imageUrls.add("https://images.unsplash.com/photo-1677708892106-d96924870618?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxlZGl0b3JpYWwtZmVlZHw0fHx8ZW58MHx8fHw%3D&auto=format&fit=crop&w=500&q=60");
                DownloadImagesTask downloadTask = new DownloadImagesTask(MainActivity.this);
                downloadTask.execute(imageUrls);
 
            }
        });
    }
}

 

 

As you can see in the above java code we are getting the reference of the button widget using their id which we have declared inside the xml layout file and then we are declaring the no of multiple image url’s and after that we are binding the onClick listener to the button when we click the button and inside it we are calling the downloadImagesTask class and inside that we are calling the execute() method and inside it we are passing the url's of the images.

 

 

 

Now we need to create a new java class called DownloadImagesTask and copy paste the following code

 

 

DownloadImagesTask.java`

 

 

Java
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
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.example.cameraproject;
 
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Environment;
import android.widget.Toast;
 
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
 
public class DownloadImagesTask extends AsyncTask<ArrayList<String>, Void, Void> {
 
    private ProgressDialog progressDialog;
    private Context context;
 
    public DownloadImagesTask(Context context) {
        this.context = context; // initialize context variable
    }
 
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
 
        progressDialog = new ProgressDialog(context);
        progressDialog.setMessage("Downloading images...");
        progressDialog.setCancelable(false);
        progressDialog.show();
    }
 
    @Override
    protected Void doInBackground(ArrayList<String>... arrayLists) {
        ArrayList<String> imageUrls = arrayLists[0];
 
        for (String imageUrl : imageUrls) {
            try {
                URL url = new URL(imageUrl);
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                connection.setDoInput(true);
                connection.connect();
 
                InputStream inputStream = connection.getInputStream();
                Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
 
                File directory = new File(Environment.getExternalStorageDirectory() + "/download");
                if (!directory.exists()) {
                    directory.mkdirs();
                }
 
                File file = new File(directory, "image_" + System.currentTimeMillis() + ".jpg");
                FileOutputStream outputStream = new FileOutputStream(file);
 
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
                outputStream.flush();
                outputStream.close();
 
                context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
 
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
 
        return null;
    }
 
    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
 
        progressDialog.dismiss();
        Toast.makeText(context, "Images downloaded successfully.", Toast.LENGTH_SHORT).show();
    }
}

 

 

As you can see inside this we are downloading the images from the internet and then we will also be showing the progressBar widget after downloading the images we are creating the downloads directory and inside it we are saving the downloaded images. And after that we are showing the toast message that the images are downloaded successfully.

 

 

 

Recent Posts

  • Android Java Project to Download Multiple Images From URL With Progressbar & Save it inside Gallery
  • Android Java Project to Capture Image From Camera & Save it inside Gallery
  • Android Java Project to Crop,Scale & Rotate Images Selected From Gallery and Save it inside SD Card
  • Android Kotlin Project to Load Image From URL into ImageView Widget
  • Android Java Project to Make HTTP Call to JSONPlaceholder API and Display Data in RecyclerView Using GSON & Volley Library
  • Angular
  • Bunjs
  • C#
  • Deno
  • django
  • Electronjs
  • java
  • javascript
  • Koajs
  • kotlin
  • Laravel
  • meteorjs
  • Nestjs
  • Nextjs
  • Nodejs
  • PHP
  • Python
  • React
  • ReactNative
  • Svelte
  • Tutorials
  • Vuejs




©2023 WebNinjaDeveloper.com | Design: Newspaperly WordPress Theme