Friday, March 30, 2012

Android Application to Send a File to Remote Server

This simple Android application sends a file to a remote server. In this example code server runs on local host. When the Send button is clicked the file is sent to the server.

Android Client Application
Server Application

43 comments:

  1. Android game developers have experience of developing and successfully distributing Android games. With its stunning visual design, mind-bending sci-fi soundtrack and effortless game play rules, has made Android the most immersive, engaging game developer. Thanks a lot.

    Inbound Marketing

    ReplyDelete
  2. Hi,

    Thanks for posting this great example. I'm trying to develop an app which needs to transfer a txt file from my android to a remote computer. I tried this example (copied and pasted) and it runs (Android app and Java server) but it does not transfer the file. It creates the file and it shows the message:

    Server started. Listening to the port 4444
    Receiving...

    But it doesn't receive anything. I'm not sure if there has to be any kind of permission for accessing the file on the Android phone.

    Thanks for your help.

    Alberto.

    ReplyDelete
  3. Alberto,
    No,It doesn't need any file accessing permission. If the sever says "Receiving..." it has accepted the client connection. Seems like you are trying to send a large file. For a large file it will take some time.

    ReplyDelete
  4. Hi Lakjeewa,

    I deeply appreciate your answer. Yes, on the server side says "Receiving" but it does not receive anything. It does create the file but after some time (5Minutes) it is still empty. I tried with a JPEG file (5MB) and it did not work, then I tried with a txt file (less 1KB) but it did not work either. Where should I locate the file on the phone?

    The path where the file is:
    /mnt/sdcard/DCIM/100MEDIA/file.txt or file.jpeg

    Does it work with the android emulator? because I tried it with both, emulator and phone.

    Thanks for your help and time Lakjeewa,

    Alberto.

    ReplyDelete
  5. Anyway it doesn't take 5 minutes for 5MB file. When the sever says "Receiving...", it just accept the client connection. Actually, following lines of code after line 43 read the file. I think your client (Android emulator) doesn't send the file.
    For your case, don't forget to change the file path in line 47 of client code to "/mnt/sdcard/DCIM/100MEDIA/file.txt". This code is specially for Android emulator. If you are going to try this with a phone, you will need more configurations in both client and sever side.

    ReplyDelete
  6. Hi,

    Thanks!. Yes, I did change the path and put the specific file's path. But it still does not work. What path should I use for the emulator and which one for the phone? Does it matter?

    Thanks again!.

    ReplyDelete
  7. It doesn't matter the path. The thing is if you try for phone, you should have a network connection with the sever (wired or wire-less). So, making a network is another task.

    ReplyDelete
  8. Thank's a lot but I have a doubt

    what If I wanna see the file that I send to the server.java, what should I do?

    ReplyDelete
  9. i get a android.os.NetworkOnMainThreadException T.T
    help

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  11. I am trying to send .txt file and its giving me the output but when I am sending .doc file or any image file its not giving any output...I am running this code in my Android device...

    ReplyDelete
  12. David,
    you can go to the place where you save the file in the sever and you can just open the file using relevant program. In this post, the file is jpg file and it can be open using any image viewer.

    ReplyDelete
  13. Anil,
    In this post im using a jpg image file and it works fine. Hope you have put the file name in the sever side with correct extension. (eg: .jpg , .txt , .doc)

    ReplyDelete
  14. I am running my app on android device which has the latest SDK support such as 4.0. But when I press button then my app stops and do not give any output. I want to ask that this app runs on upper versions or it runs only on lower versions...???

    ReplyDelete
  15. This application is programmed only for emulator which is working in local host environment. If you want to run this application in a device you should build a connection between the server and the device. It is an additional task which needs more effort. You can build a connection via web or Bluetooth technology.

    ReplyDelete
  16. Thanks for your reply always...I am developing an application in which from my mobile I am sending files to the WiFi printer by IP address and port number, which are .txt, .png, .jpg, .doc. They should be printed from the printer. I have tried your code but it is giving me output for .txt file only. What changes are required to get output of all type of files??? Please help me...
    Its my project...
    Once again thank you...

    ReplyDelete
  17. I'm getting this error, "emulator-5554 disconnected! Cancelling 'com.example.filetransfertoserver.SimpleClientActivity activity launch'!" any help?

    Thanks!

    ReplyDelete
  18. after debugging I found why it stands as receving an never finishes
    update this variable with your file size.!!! otherwise it wont finish
    private static int filesize = 10000000; // filesize temporary hardcoded

    ReplyDelete
  19. Hai,
    Really awesome blog.
    I need a help how to send live recorded video to remote server from our android smartphone.
    kindly help me.

    ReplyDelete
  20. I am getting Error : Please advice

    dalvik.system.basedexclassloader.findclass classnotfoundexception

    ReplyDelete
  21. As I followed same step which u have been explained in above

    ReplyDelete
  22. Can you upload code for sending files or even text to remote (publicly available) machine. So basically the app send data from smartphone to server (one way communication is fine for now).

    ReplyDelete
  23. Thanks a lot for the post , it is really helpful. You saved my day

    ReplyDelete
  24. @Alaa smarneh
    can you please mail the .apk to me...
    shivam.pesit@gmail.com

    ReplyDelete
  25. Very nice Blog..
    I have one simple question...how do we check this code..
    android code in android phone and server code in laptop??
    silly question but will be very helpful for me...
    Thank you

    ReplyDelete
  26. Hi all ,dear lakjeewa
    do we use same for video .ogg etc.

    ReplyDelete
  27. hey Madhuranga Lakjeewa ..excellent work..... i m trying to run this code but i m getting few errors ...... can u plz provide download link for this code ...my mail id is aajay.28@gmail.com

    ReplyDelete
  28. i implement it with two emulator , and i have no error
    but it's just print receiving !
    and i didn't receive anything :S

    ReplyDelete
  29. ne3ma.rayan91@gmail.com
    can you email me the source code plz

    ReplyDelete
  30. You kep saying for this code to work on phones we need to establish connection. Can you please tell me how t establish connection between an android device and a laptop both wid their own net connection (not sharing the same ip).

    Thank you in advance.

    ReplyDelete
  31. why IP 10.0.2.2 I change Localhost no run?

    ReplyDelete
  32. Is there a way to not have the file hard coded in and have the app locate it in the phone's directory?

    ReplyDelete
  33. Where does the server run? In the emulator?
    Sorry if my question is lame..I'm a newbie

    ReplyDelete
  34. Hi...i was trying to execute the above code and noticed that the apk gets installed (\SimpleClient\bin\SimpleClient.apk installed on device) but does not appear in the emulator...Also can you please let me know how to establish a connection to transfer a file from pc to android as i am new to android.

    ReplyDelete
  35. i suppose im lucky to have found this! I hope you will be adding more in the future...
    facetime

    ReplyDelete
  36. Buddy, ... suppose i received an file from server , and i want it to preview in my client app , and for that purpose i need to call the "open with" facility of android to open and preview that file in that client app ...
    for ex: i recieved an image or a text file from server and i want to preview it in the app , so on click of preview button that should show "open with" dialog box and give the options of all the file viewer present in that device for that particular file , i.e. it should give me picasa or gallery option for an image file .....

    ...
    so can u give me any idea ... how to achieve this kind of things ..
    m strucked in my project right now ....
    thnx in advance ...

    ReplyDelete
  37. Hi guys,

    Do you have any idea about to fetch android system file (ex: log) by using server programming??? Make sure android system must not aware of fetching... Don't think it is for illegal purpose, i am a researcher need to work on some data analysis

    ReplyDelete
  38. Thank you very much for this tutorial. However I got a 'network on main thread exception' but solved it using AsyncTask :)

    ReplyDelete
    Replies
    1. Great to see someone from SL. :)
      Yeah Lakshitha, Above tutorial is based on Android API Level 10. But API Level 11 introduced AsyncTask implementation for network communication. Therefore this tutorial is now deprecated. As you found AsyncTask is the solution and then it works. :)

      Delete
  39. Hello my friend... first as sending messages and files after the same socket
    how achieve

    ReplyDelete
  40. Thanks very nice blog!

    ReplyDelete
  41. I go to see daily a few blogs and sites to read content, however
    this webpage presents quality based content.

    ReplyDelete