Android Upload Video to Server using PHP

Many peoples requested me to write a tutorial to upload video to server. So in this android upload video to server tutorial we will learn how we can upload video from android gallery to  our web server.

For this tutorial I will be using Hostinger’s fee web hosting server.

Some peoples also asked me about a tutorial to upload files to my website. So, you can also use this for File Upload Server Application with a little modification.

So the first thing I need to do for this android upload video to server project, is to create a php script that will handle the multipart request sent from our android device.

Creating Server Side Scripts

  • Go to your hosting (You can also use wamp/xampp) and create a directory. (I created VideoUpload).
  • Now inside the directory create one more directory named uploads.
  • And create a new file upload.php, and write the following php code.

  • Note down the URL for upload.php and thats all for the server side.

Android Upload Video To Server Project

  • Open android studio and create a new project. I created VideoUpload.
  • Now come to activity_main.xml and write the following xml code.

  • The above code will produce the following layout.
android upload video to server
android upload video to server
  • Now we will create a new class to handle the upload.
  • Create a new class named Upload.java and write the following code.

  • The above code will simply send a multipart request to the URL specified. And will return the response from the server as a string.
  • Now come to MainActivity.java and write the following code.

  • Now add READ_EXTERNAL_STORAGE and INTERNET permission to your manifest file.

  • Now run your application.
android upload video to server
android upload video to server
  • And yes it is working absolutely fine. You can get my source code from the GitHub repository. Just go to the link given below.

Get Android Upload Video to Server using PHP Source from GitHub

Some more Android Application Development Tutorial to Check 

So thats all for this android upload video to server tutorial friends. I haven’t explained the code because we already have seen many tutorials like this. But still if you are having any query regarding this android upload video to server tutorial leave your comments. Thank You 🙂

80 thoughts on “Android Upload Video to Server using PHP”

    • What is meant by making directory. Where should i make the directory. Im a little confused about it. Thanks in advance

      Reply
  1. Hii Belal,

    I want to save video url in database with some parameter like name and address. here is the code with some change .Those changes i have made in “android-upload-video-to-server-using-php ”

    public static final String KEY_TEXT = “pondid”;
    public static final String KEY_TEXT1 = “name”;
    public static final String UPLOAD_URL = “http://192.168.0.113/userregistration/uploads.php”;

    //onCreate()
    e_pondid=(EditText)findViewById(R.id.e_pondid);
    e_name=(EditText)findViewById(R.id.e_name);

    //uploadVideo()
    final String pondid = e_pondid.getText().toString().trim();
    final String name = e_name.getText().toString().trim();

    //doInBackground()
    RequestHandler rh = new RequestHandler();
    HashMap param = new HashMap();
    param.put(KEY_TEXT,pondid);
    param.put(KEY_TEXT1,name);

    String result = rh.sendPostRequest(UPLOAD_URL, param);
    return result;

    Reply
    • Hii Ashwini,
      I also have same doubt, I want store Video URL with some attributes like name, description.
      How to send the name and address parameters from java coding.

      Thanks,

      Reply
  2. Great Tutorial sir.

    it show file uploaded successfully but i am not getting any file over the server.
    tried changing permissions, but still not able to understand whats wrong in that.

    Reply
  3. hey bro..!! i have one doubt…I have made one simple app…Which connects to server…I am accessing it through my ip…aftr some time am running that app it is not working…then i have checked my ip….it has to something else…my system ip meant…
    plz do help me…
    whats is happening here…that s static one…hw tat suppose changed… make it clear

    Reply
    • I seen this situation once but never bothered about thinking why it happened. My wamp server’s ip was changing in every restart.. but now it is static and I haven’t done anything…
      You have to change the ip which is working in the url

      Reply
  4. Dear Bilal..I just wanted to thank you for your effort you are putting to teach people stuff you worked hard to learn..
    with the help of your blog i have started my own android developer job:), and it means alot since i’ll be able to bring my family from war .
    thanks alot, and you will see me commenting and supporting alot:)
    BTW…the best thing in your blog is that you actually reply and listen to the readers … thanks alot dude you are the best 🙂

    Reply
  5. Hello Belal

    First of all, thank you for your post. I am trying to make an application which uploads a video to the server based on your code. When I try to upload a video, I get a string response successfully. But the thing is I actually do not manage to upload the file itself. I’m trying to find out what the problem is with my code, but there is a limit. So I need your help. If you don’t mind, could you visit my GitHub account and check it out?

    The Android application link is https://github.com/marshallslee/VideoStreaming
    The classes that are involved in uploading the videos are VideoManager.java (the class which uploads the video, link: https://github.com/marshallslee/VideoStreaming/blob/master/app/src/main/java/com/marshall/videostreaming/network/VideoManager.java) and UploadAVideoActivity.java (the UI class, link: https://github.com/marshallslee/VideoStreaming/blob/master/app/src/main/java/com/marshall/videostreaming/ui/UploadVideoActivity.java).

    and the php-side link is https://github.com/marshallslee/VideoStreamingBackend/tree/master/VideoStreaming.

    If you have some time to check it out and find out what the problem is, that will be very much helpful for me. Thank you in advance.

    Reply
  6. Hello Belalo thanks alot for your wonderful tutorial again 🙂
    Every thing you post works perfectly 🙂
    Now, I have a small question,I was trying to send a String from android with the video.
    In your past tutorial :
    https://www.simplifiedcoding.net/android-upload-image-from-gallery-with-text/

    you managed to send the text as a hashmap:

    OutputStream os = conn.getOutputStream();
    BufferedWriter writer = new BufferedWriter(
    new OutputStreamWriter(os, “UTF-8”));
    writer.write(getPostDataString(postDataParams));

    writer.flush();
    writer.close();

    I tried to implement this in this code (with the getPostDataString sure) but i failed , so i had to use this other way,

    dos.writeBytes(twoHyphens + boundary + lineEnd);
    dos.writeBytes(“Content-Disposition: form-data; name=\”name\”” + lineEnd);
    dos.writeBytes(lineEnd);
    dos.writeBytes(Config.NAME); // NAMEis String variable
    dos.writeBytes(lineEnd);

    which is working good but i had to re do this for every string i am gonna send, I was wondering if you can tell me how to implement your old code into this one,,, (not the fully code and sure not the main activity code My problem is only with the sendpostrequest method 🙂
    And i know you have exams and no hurry my friend take your time and god bless you.

    Reply
  7. Bhai pahele to thank sooo much your tutorials are worth for the students like us u make it soo easy and no need to download any external library or any other stuff thnx through your tutorials I learned to post text,images on wamp and receive back thnx to u for that there is no other easy way to this except ua tutorials .. now I here with 1 problem of storing videos on server every thing is working fine just when the video is uploaded from app the response from php is ” undefined myFile” how to resolve this

    Reply
  8. Hi man, Thank you for your posts. I was wondering, how would you use this very code to add more parameters to the data sent from upload.java to php script. I’m basically trying to upload a video with a comment at the same time.

    In one of the posts where you upload to the server you did something like this;

    @Override
    protected String doInBackground(Void… params) {
    RequestHandler rh = new RequestHandler(); already created up. But if it leads to errors use it
    HashMap param = new HashMap();
    param.put(Config.KEY_USR_ID, userId);
    param.put(Config.KEY_CMT, cmt);
    String result = rh.sendPostRequest(Config.URL, param);
    return result;
    }

    I can’t get this to work in this particular example.

    Reply
  9. Thank you for the well explained tutorials. Really nice work. This code also works for me. But I want to add the video to database. I tried to do this. Can you explain simply the way to do this? I made a android project by following your ‘upload image to server’ with some changes. I created php file too.But getting errors.i think the error comes because of php file. Please help me on this.

    Reply
  10. Hi Belal,

    Thanks for your great Tutorial. I am beginner to android and for client – server Communication. My Doubt is, Can I use My Xampp Localhost IP to connect with my android, for fetching data to server and getting responses. If YES please send me the procedure. with one small example to email id(venkat.madhu03@gmail.com)…

    Thanks in Advance for your help…

    Reply
  11. Hi Belal,
    I am use your code for upload the mp4 file on server, it uploaded successfully but video file is not playing, so what can do for this problem.
    Thanks

    Reply
  12. Hi, I’m trying to add a EditText to put a name, and add not get to send it by post with the video.
    Someone can help me ?, thanks

    Reply
  13. This is real close to what I am wanting to do, however, I need a little more functionality.
    Is there a way, I can make it where users have to enter their User Name and Password for the server in to the app, have it verify them on the server, then have a button to live record the video, then upload to the server, but put it in their personal video folder. I will also need to add the video to the MySql Database.

    Doable?

    Reply
  14. hi Belal thi is nice tutorial to learn android , the coding was easy to learn with clear example , thank u so much belal

    Reply
  15. Hi belal code is working at one device using Linux; U; Android 4.2.2; Build/JDQ39
    but at another device Dalvik/2.1.0 (Linux; U; Android 5.1; Build/LMY47D)
    it does not work.

    I am sure problem is in php file.
    but i am able to see some echo message come from php file to app

    Kindly solve the issue thnx

    Reply
  16. Working fine, but it would be better if video is taken through camera….pls send a code to take video through device and upload it to server.

    Reply
  17. What means int maxBufferSize = 1 * 1024 * 1024?? It this work for large files?

    How can I define as 30 seconds of video ? Or for example 10MB of video?

    Reply
  18. Hi,

    Awesome work. How can we add a text box and put both text box value and the video URL to the mysql database?

    Thank you.

    Reply
  19. Hi
    Tnx for your article.
    I try upload multi file and most of them getting this error in android :
    “Caused by: java.lang.OutOfMemoryError: Failed to allocate a 65548 byte allocation with 38840 free bytes and 37KB until OOM”

    Reply
  20. after click button upload , i want to show in listview, but i don’t how to add thumbnail video to imageview in listview? ,can you help me ?

    Reply
  21. Hi,
    i want to change the filename of the uploaded file to a custom one (read out from an edit text). I tried to pass a string with POST to the php script, but could not get it working yet.
    Can u help me and show me how and at which place i have to add the code in java? I used URLEncoder.encode with UTF-8, but again, could not get it working yet.
    Greetings and thanks,
    Hendrik

    Reply
  22. Hey great work buddy! Can you please tell me the modifications that need to done in this code if i want to upload my video to my online server?

    Reply
  23. Caused by: java.lang.SecurityException:
    Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/video/media from pid=2582, uid=10081 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()

    In the AndroidManifest :

    Reply
  24. I do not know why always “could not upload” when using the app in my android phone. However, the video can be uploaded successfully in AVD.

    Reply
  25. Bro can u tell where use the key and if u dont use key then please how can i did this with key because i want other field like string and image to send simultaneously

    Reply
  26. Hi,
    i tried your code…………. it runs properly without any error, but there is bug. when i m selecting video file. i debug the code , it stopped on ……
    private String getPath(Uri uri) { Cursor cursor = getContentResolver().query(uri, null, null, null, null);
    cursor.moveToFirst();
    how can i fix it…

    thanx.

    Reply
  27. These codes do not work on version 5 of android.

    selectedPath = getPath(selectedImageUri);
    and
    cursor.moveToFirst();

    It says there are some mistakes in the parts.

    Reply
  28. Heyy Bilal,

    I have tried your code it only stores the name of file on the server not the complete file. Is it from program or there is any mistake?

    Thanks in advance

    Reply
  29. Hi Bilal, i am getting message, uploaed at “MY_URL”, i found nothing in my folder on the server, could you please still help out. Thanks a lot.

    Reply
  30. Hi Belal,

    I have downloaded your code and using xampp server and running on my local machine. After clicking on upload button get below error. could you please help me fix it. thanks.

    failed to connect to localhost/127.0.0.1 (port 80): connect failed: ECONNREFUSED (Connection refused)

    Reply
  31. Hi Bilal,

    you are always guiding with wonderful tutorials and i used this and it is working fine. I still have few more queries, it would be great if you can help.

    1. In your PHP location is uploads/, i want to keep it dynamic and send as one parameter along with video upload request. Please guide how to do it?
    2. Your code worked fine on my Hostgator server but when i tried on Godaddy, no file is being uploaded. Can you guide possible fix required for Godaddy.

    Reply
  32. yourapp is trush firstable most important part which you didnt add download file which you share is php file and second your app doesnt upload larger files it is only 1 mb file uploaded

    Reply
  33. Thanks so much for your tutorial, please can you work on Kotlin version of this tutorial (Android Upload Video to Server using PHP), with Retrofit saving video url in database with some parameter like name , address, description.

    Reply

Leave a Comment