Android: Uploading File to Server with Progress Bar

In this tutorial, I will explain how to upload any kind of file from your Android app to PHP server showing the percentage of progress. To begin with, let’s code the server side.

Creating PHP Project

1. Install WAMP/XAMPP server if you haven’t installed earlier in your desktop.

2. Now open www folder of WAMP server (htdocs in case of XAMPP) and create a folder named api and upload_files folder inside the api folder.

3. Inside the upload_files folder, create a subfolder named files. This folder will contain all the uploaded files.

4. Now inside the upload_files folder, create a file named index.php and add the following code.

Here we check if the there is any file getting posted. If yes, then we move the uploaded file to files directory and populate the response with the path of the uploaded file. When an error occurs, then we set an appropriate error message in the response.

5. Open php.ini file from your XAMPP server (From XAMPP Control Panel, Click on Config => PHP (php.ini))

Updating PHP_INI in XAMPP

In WAMP server, from the icons tray, click on WAMP => PHP => php.ini.

Updating PHP_INI in WAMP

6. Now update the following values. This is done so that we can upload large files (up to 50MB). You can change the values based on your need.

7. Now restart WAMP/XAMPP server.

Creating Android Project

1. Create a project named Upload File To Server.

2. Add the following dependencies in the build.gradle file:

3. Add required permissions to AndroidManifest.xml:

4. Create a new XML file inside the drawable folder with the name ic_file.xml and update it with the following SVG code. This is a vector graphics icon used to display file icon as a preview when a non-image file is chosen to upload.

5. Add the string resources used in the project to strings.xml

6. In activity_main.xml, add 2 buttons, one for choosing file and another for uploading file. 1 ImageView for showing file preview and 1 TextView to display file name.

7. Create a custom class named MyHttpEntity, which extends HttpEntityWrapper. This helps in calculating the progress percentage while uploading the file.

8. Finally, update the MainActivity with the following code. Here when the user clicks on Choose File button, the app asks permission for reading files from the external directory. When the user grants the permission, it prompts with the options like Gallery, File Manager etc so that user can choose the file. Once the user chooses the file, File name and Preview will be displayed. Choose file button will be hidden and Upload file button will get displayed. When the user clicks on the Upload button, the file starts getting uploaded to the server showing the progress percentage.

Here the IP address 192.168.43.72 and port 8888 are my machine IP and ports. You may update it with your machine IP or server domain URL.

Now run the application on your Android device or emulator.

Demonstration

Below is the demonstration of the app:

Upload file to server

Source Code and APK Files

You can download the Source code and APK files from the below links:

Download Source CodeDownload APK

If you have any doubts, then we can discuss them in the comments section. If you have liked this tutorial, please do follow us on Twitter and Facebook.

Stay Connected

No spam guarantee.

Abhishek

Abhishek

Abhishek loves coding. His favorite is Android Development. Apart from coding he loves traveling and reading.
Abhishek

Leave a Reply

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