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 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.
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.
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.
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.
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.
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?
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.
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...
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.
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)
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...???
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.
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...
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
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).
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
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
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).
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.
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 ...
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
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. :)
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.
ReplyDeleteInbound Marketing
Hi,
ReplyDeleteThanks 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.
Alberto,
ReplyDeleteNo,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.
Hi Lakjeewa,
ReplyDeleteI 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.
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.
ReplyDeleteFor 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.
Hi,
ReplyDeleteThanks!. 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!.
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.
ReplyDeleteThank's a lot but I have a doubt
ReplyDeletewhat If I wanna see the file that I send to the server.java, what should I do?
i get a android.os.NetworkOnMainThreadException T.T
ReplyDeletehelp
This comment has been removed by the author.
ReplyDeleteI 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...
ReplyDeleteDavid,
ReplyDeleteyou 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.
Anil,
ReplyDeleteIn 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)
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...???
ReplyDeleteThis 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.
ReplyDeleteThanks 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...
ReplyDeleteIts my project...
Once again thank you...
I'm getting this error, "emulator-5554 disconnected! Cancelling 'com.example.filetransfertoserver.SimpleClientActivity activity launch'!" any help?
ReplyDeleteThanks!
after debugging I found why it stands as receving an never finishes
ReplyDeleteupdate this variable with your file size.!!! otherwise it wont finish
private static int filesize = 10000000; // filesize temporary hardcoded
Hai,
ReplyDeleteReally awesome blog.
I need a help how to send live recorded video to remote server from our android smartphone.
kindly help me.
I am getting Error : Please advice
ReplyDeletedalvik.system.basedexclassloader.findclass classnotfoundexception
As I followed same step which u have been explained in above
ReplyDeleteCan 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).
ReplyDeleteThanks a lot for the post , it is really helpful. You saved my day
ReplyDelete@Alaa smarneh
ReplyDeletecan you please mail the .apk to me...
shivam.pesit@gmail.com
Very nice Blog..
ReplyDeleteI 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
Hi all ,dear lakjeewa
ReplyDeletedo we use same for video .ogg etc.
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
ReplyDeletei implement it with two emulator , and i have no error
ReplyDeletebut it's just print receiving !
and i didn't receive anything :S
ne3ma.rayan91@gmail.com
ReplyDeletecan you email me the source code plz
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).
ReplyDeleteThank you in advance.
why IP 10.0.2.2 I change Localhost no run?
ReplyDeleteIs there a way to not have the file hard coded in and have the app locate it in the phone's directory?
ReplyDeleteWhere does the server run? In the emulator?
ReplyDeleteSorry if my question is lame..I'm a newbie
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.
ReplyDeletei suppose im lucky to have found this! I hope you will be adding more in the future...
ReplyDeletefacetime
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 ...
ReplyDeletefor 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 ...
Hi guys,
ReplyDeleteDo 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
Thank you very much for this tutorial. However I got a 'network on main thread exception' but solved it using AsyncTask :)
ReplyDeleteGreat to see someone from SL. :)
DeleteYeah 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. :)
Hello my friend... first as sending messages and files after the same socket
ReplyDeletehow achieve
Do you want to send the message and the file at the same time?
DeleteThanks very nice blog!
ReplyDeleteI go to see daily a few blogs and sites to read content, however
ReplyDeletethis webpage presents quality based content.