Android Upload File to Server

Android Upload file to server using PHP, MySQL

Vamsi Tallapudi Android, Programming, Tutorials 65 Comments

In this Android Upload File to Server tutorial, we are going to discuss about how to select a file in android and upload it to the server using New Android Studio. Using the following code, we can successfully upload images, videos, documents, zip files, etc.. to the server. The code is tested for files upto 10MB of size.

android upload file to server featured logo

Note:

The link to download the complete code for the following tutorial is given at the bottom of this article.

Android Upload file to Server:

For this tutorial, I have created a New Project Named UploadFileToServer. Now let us first declare the permissions required in our Android Manifest file.

The above permissions are required to access Internet and to Read Android External Storage files.

I have written the following code in activity_main.xml:

Now create a java class named FilePath.java and add the following code to it:

The above class is used to return the file path of the selected document/ video / audio etc..

Now we declare the MainActivity.java as follows:

The above code is commented wherever it is necessary. In this MainActivity.java, we wired up the attachment icon, Textview and Upload button and coded them such that when the icon is clicked, we can select the required file so that the textview gets updated with the path of the selected file. When we click on Upload button, a ProgressBar appears indicating that the file is being uploaded to the server. After the upload, the ProgressBar disappears and the textview gets updated detailing where the file has been uploaded in the server.

Server Side PHP Script for Android Upload File to Server:

Now create a serverside PHP Script that allow Android Upload file to server. Here I named it UploadToServer.php and added the following code:

I have placed the above file in Coderefer.com/extras folder. The above code creates a new folder named uploads and places the files being uploaded to uploads folder.

The complete code to demonstrate Android Upload file to server can be cloned/downloaded from the github repo below:

Click here to download

Note: In order for the above code to work, you need to  place php file (given in the project) in your own server/testing environment.

Related Posts