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 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).


  • 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.


  • 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.

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.

68 thoughts on “Upload PDF File to Server in Android with PHP and MySQL”

      • do i need to change the kitkat wala code to implement it for lollipop or marshmallow ?

        “//check here to KITKAT or new version
        final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

        // DocumentProvider
        if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {

      • So what code shud I write to replace this, so that I can use android marshmallow (api 15)?

        //check here to KITKAT or new version
        final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

        // DocumentProvider
        if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {

          • Did you try the above code on ice cream sandwich? If not ,then go ahead and do it. If you did, tell me the exact problem?

          • On kitkat, it works perfectly. But on icecream sandwich ( minSdkVersion 15) it says “could not find file at path: file:///mnt/extSdCard/myfile.pdf” – that is when I click upload.

  1. hi Danish,
    i get this popup(“no application can perform this action”) when i click on “select” to select pdf file…why? THANKS

  2. hi thanks for the tutorial . Please can you make a tutorial on writing to SD card in android 20 or 21 plus that is downloading straight to SD card and reading from SD card .. that would help a lot because its been a challenge for some of us.

  3. Thanks Brother,
    Very nice project… help me a lot….
    Can you add Download manager to this project? after fetching all files its work likes
    /*** if file exists (View for Download) ***/
    // if file not exists –> download first

  4. i think theres a problem with upload … cause im trying but the file im uploading keep on overlaps to another file that i first upload.. i already uploaded 10files still the server has 1file … 🙁 please help me.

  5. Thank you so much. from two days I have an issue to upload a file can’t able to solve it. With the help of your code Its possible … U are greate

  6. Manifest.permission.READ_EXTERNAL_STORAGE is showing error. MultipartUploadRequest and UploadNotificationConfig are also showing error.

  7. I am getting the exception, W/System.err: org.json.JSONException: Value response of type java.lang.String cannot be converted to JSONObject

  8. I am not able to fetch the pdfs.It keep on fetching.But at serverside,its working .And fetching the records.I used postman to check it.

  9. I got stuck at upload.php where isset($_POST[‘name’]) and isset($_FILES[‘pdf’][‘name’] is always false when tested using Postman. Please help.

  10. sir i am not able to understand what is the table name

    you wrote this – i am confused between notestacker and pdfs , please help me in this

    $sql = “INSERT INTO notestacker.pdfs (id, url, name) VALUES (NULL, ‘$file_url’, ‘$name’);”;


  11. Sir
    i am able to insert pdfs in mysql database, when i click on fetch button it displays links of the pdfs , when i click on the link to get pdfs , i get a message “THIS PDF COULD NOT BE OPEN”

    please help me to retrive my pdfs


    • Hi Samir , I am using the same Android code and the same Url provided . While uploading i am getting an error like “Error During Upload” Can you help me Please

  12. I think a lot of people got this problem “Error during Upload” someone had the same issue? could you share some tips? thank you

    • I Solved that . The reason is URL provided by the author is not working , we need to write our own url with the same code the author has provided

  13. Hi Belal Sir,
    Can you send me the apk file for this application.L am in dire need for the apk file for my college project

  14. we are using 000webhost for server, we are using the steps as given above. the file is getting uploaded and it is showing a successfull message but in our uploads folder, the file is not present. Can you tell where should we keep the dbdetails.php and upload.php files so that we can get the output.

  15. Sir i am getting the upload successful message but there is no such pdf in the root directory and none entry in the php database

  16. Hii bilal ,this helps me for uploading the file to the server…………but I Reuired to run The ProgrssDialog and Dissmiss That Progress Dialog On Successful Ulopad………..

  17. Hi Manish,

    I want to create an android application of PDF Reader that can fetch PDF files from the server (PHP and MYSQL)


    Pardeep Patel

  18. hi.. If I want to upload file from sd card its not getting selected. It works with internal memory. If I want file from external memory what to do?
    Now I am getting message like “Please move your .pdf file to internal storage and retry”

  19. i am trying to select pdf form android emulator but i don’t get any item in emulator how can i am selecting pdf form android emulator

  20. hi.i can upload files.but i cant’t fetch them.in case i get this :
    “error”: false,
    “message”: “PDfs fetched successfully.”,
    “pdfs”: []
    what is the problem?

  21. click fetch pdf button but does not display list of uploaded pdfs list & im also check my php file it working&disply list
    plz i want to list of pdf file plz sir help me

  22. what should i change if i want to show or download specific pdf instead of array ?
    what should i change in the code ?

  23. Sir…
    How to solve this Error?? I cant Solve this error…

    Program type already present: android.support.v4.app.BackStackState$1

  24. path==null so print toast “Please move your PDF file to internal storage & try again.” so please help whay this occur

  25. Hello…what can I do if I’d like to fetch specific documents?
    eg fetch WHERE ID = ?

    HOW can I achieve that on android studio.
    Still on this is it possible if you can make uploading and downloading tutorial with retrifit?
    Thank you in advance.


Leave a Comment