Upload PDF File to Server in Android with PHP and MySQL

Hi there, today’s  lesson is about Android Upload Service. Android Upload Service is a very simple and efficient way to achieve the target of uploading files of different extensions such as .pdf, .docx, .xls and …

upload pdf file to server in android

Hi there, today’s  lesson is about Android Upload Service. Android Upload Service is a very simple and efficient way to achieve the target of uploading files of different extensions such as .pdf, .docx, .xls and so on and use them as per required. In this example, I”ll  show you how to upload pdf file to server in android. So let’s get started.

Upload PDF File to Server in Android Tutorial

Setting the server for the task

  • To set the server for this task, I am gonna use PHP with MySQL. So, go to localhost/phpmyadmin  and a create a database as shown below. (I am using wamp server, you can use any other like xampp or even a live server as well).

database

  • Inside your server’s root directory (c:/wamp/www) and create a new folder. I created AndroidPdfUpload.
  • Inside the folder create a folder named uploads, in this folder we will save all the uploaded PDFs.
  • Now, create a php file named dbDetails.php and add  the  following code. It stores all the constants to be used later.

  • Next, create another php file named upload.php and add the following code.

  • With this, your server is all set for your android project. But before getting into android side you can test your server for file upload.
  • For testing you can use any REST Client. I am using postman.
upload pdf to server in android
Upload PDF File to Server in Android
  • As you can see the upload is working fine at server level. Now this is the time to code our Android Application.

Creating Android Project

  • Create a new android project with Empty Activity. As the files get loaded and the gradle is built, begin with adding the dependency to the app level gradle which enables the project to use android upload service and then sync it.

  • Now, configure the MainActivity.java and activity_main.xml as shown below.
  • Here’ the MainActivity.java. It consists of a method named as uploadMultipart() which carries out the upload task. It also consists of storage permissions which is required for this project as it involves reading the android storage.

  • Here’s the activity_main.xml. It consists of few buttons and an EditText to take the input of the pdf file and filename.

  • The above code will generate the following layout.

xmllayout

  • Next, create a class named FilePath.java and add the following code. This class provides us with a method to fetch the absoulte path of the selected pdf which is required by the method uploadMultipart() in MainActivity.java.

  • We’re almost done. Just add the Internet permission to you AndroidManifest.xml file as  shown below.

  • And you did it. Run the app now, select the pdf and enter a name for it and the upload shall begin. The url used in this project is live. Once pdf  upload is complete, its gonna flash a message saying file uploaded successfully. The sample output can be seen in the below images.
upload pdf to server in android
Upload PDF to Server in Android

Fetching PDFs From Server

  • To fetch the uploaded pdfs from the server, we need a php file to process this request. So, go ahead and create a php file named getPdfs.php and add the following code to it.

  • With this, the server is ready to facilitate the fetching of pdfs on the app. Again you can execute this script using POSTMAN.

fetch pdf from server android

  • As you can see we are getting the id, url and name of the uploaded pdfs in JSON format. We will read this data in android and then we will download the PDF with the URL.
  • Now, let’s  begin  the work  on the  android part. To get started, begin with  configuring  the activity_main.xml file. To initiate the fetching of pdfs, we need a button for the same and also a listview to display the list of pdfs fetched.
  • To do so, update your activity_main.xml file with the code shown below:-

  • As you can see that the above xml code consists of a ListView and that calls for another layout resource file which would server as the layout of the ListView. So,  go ahead and create a layout resource file named list_layout.xml and add the following code.

  • It simply consists of two TextViews to show the name and url of the fetched pdf in the ListView.
  • Now that we are done with the xml part, its time to configure the Java section of this project. To do so, begin with creating a java class named Pdf.java and add the following code.

  • The above class is the model class for Pdfs and will be used to manipulate the name and url of Pdfs.
  • Next, to configure the ListView, we need to define a custom adapter. So, go ahead and create a class called PdfAdapter.java and add the following code.

  • Now, we need to configure the MainActivity.java to make the final moves. To do so, go ahead and add the following dependency to your app level gradle and then sync it.

  • The above move just enabled the use of Volley in this project which will be used in the method which would fetch the data from the server.
  • Finally, update your MainActivity.java with code below.

This update includes

  1. Declaration of Fetch Button, Listview, ArrayList of Pdfs, ProgressDialog, PdfAdapter, url to fetch pdfs and their intializations
  2. Setting on click listener to Fetch Button and ListView.
  3. Defining the method getPdfs() which fetches the data from the server.

  • And you did it. Run the app now and hit the fetch pdf button to see the list of uploaded pdfs. When clicked on any List Item, it downloads the pdf for you. The sample output of this part of the project can be seen below.

upload and fetch pdf

  • Now if you want the source code of this Upload PDF File to Server in Android Tutorial then you can get it from the below link.

[sociallocker id=1372] Upload PDF File to Server in Android Source Code [/sociallocker]

So thats all for this Upload PDF File to Server in Android Tutorial. And if you are having any query or doubt regarding this Upload PDF File to Server in Android Tutorial meet me in comment section.

Hello, I am Manish Kumar. I am a B.Tech Student at NIT Jamshedpur. Fortunately, I find myself quite passionate about Computers and Technology. Android is my most recent Crush.

Expand Your Knowledge: Next Tutorial Picks

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x