Example: upload files to php from android

by octagon » Wed, 22 Oct 2008 09:25:08 GMT

Sponsored Links
 his is a way for android to POST a file upload to a php script. I had
a bit of trouble figuring out the ins and outs of the http client
situation, but this is what works for me (hope someone finds it


Expect/continue handshaking needed to be disabled to avoid getting 417
errors from lighttpd. Doesn't work without an sdcard yet, as there is
no Content-Length header associated with uploading an OutputStream as
opposed to a File object, and writing to files is only allowed on the
sdcard (as far as I know, please correct me if there is a way to do




<form enctype="multipart/form-data" action="upload.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br /
<input type="submit" value="Upload File" />
$to_file = "tmp/" . basename($_FILES['uploadedfile']['name']);
$from_file = $_FILES['uploadedfile']['tmp_name'];

if (move_uploaded_file($from_file, $to_file)) {
echo "Successful upload";
<a href="<?php echo $to_file;?>"><?php echo $to_file;?></a>
} else {
echo "Unsuccessful upload";


import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

public class DemoActivity extends Activity {

/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {

* Make a simple view with a button and a bit of text. Click
the button to upload the file to the
* server. The file will be saved to tmp/test.txt (relative to
your php script) and it should contain
* the current time and date.
final TextView tmp = (TextView) findViewById(R.id.textView1);
tmp.setText("Hi! Click the button!");

Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
File f = new File("/sdcard/test.txt");
try {
Date d = new Date();
PrintWriter writer = new PrintWriter(f);

Example: upload files to php from android

by De San Nicolas Jean Philippe » Fri, 24 Oct 2008 05:45:49 GMT


just a question (for the moment -:))

you set the dependencies in your classpath in your Android project?


2008/10/21 octagon <[EMAIL PROTECTED]>


Sponsored Links

Example: upload files to php from android

by octagon » Sat, 20 Dec 2008 03:20:37 GMT

 orry, I didn't see your question! In case anyone has the same
problem, the answer is yes. I am using eclipse. I first imported the
jar files into my project so that the whole thing is all in one place.
I put them in res/assets/dependencies.
Then do:

1. main menu Project --> Properties
2. select Java Build Path from left pane
3. go to Libraries tab
4. click Add JARs

hope it helps someone out.

On Oct 23, 4:45pm, "De San Nicolas Jean Philippe" <jph...@gmail.com>

Example: upload files to php from android

by arnouf » Mon, 19 Jan 2009 20:57:09 GMT

 i all,

I used this method until now. But I would like to light my application
on device - without apache libraries - to use the code present in this

It doesn't work...Could you help me?

For information I would like to send an xml file (so text) and not a


On Dec 19 2008, 8:20 pm, octagon <micha.nis...@gmail.com> wrote:

Example: upload files to php from android

by arnouf » Mon, 26 Jan 2009 17:35:33 GMT

 t's done :)

On 19 jan, 13:56, arnouf <arnaud.far...@gmail.com> wrote:

Example: upload files to php from android

by AKD » Sat, 07 Mar 2009 10:53:19 GMT

 i arnouf could you plz provide your code. i need to work on same
task. thanks in advance.

On Jan 26, 2:35pm, arnouf <arnaud.far...@gmail.com> wrote:


Other Threads

1. ApplicationContext.java and Activity.java. Is ApplicationContext not used ?

ApplicationContext is private implementation.  Activity takes care of the
startActivity implementation for itself, since it wants the starting
activity to be associated with the original activity (and ApplicationContext
is just the most generic context, which doesn't know that it is an

Dianne Hackborn
Android framework engineer

Note: please don't send private questions to me, as I don't have time to
provide private support, and so won't reply to such e-mails.  All such
questions should be posted on public forums, where I and others can see and
answer them.


2. problem installing a .apk file

hi ! I am a newbie and wanna install a barcode app on emulator. The
app is moseycode and i have its .apk file. when i run 'adb install
<path>' in command prompt this error is displayed ..


i dnt know why this error is here !! :S plz help


3. help with rawQuery statement

4. Launch date/time settings from widget

5. How to properly use PreferenceActivity.addPreferencesFromIntent(Intent)

6. Accelerometer: Fixed Sample Rate Requirements

7. How to display list of images in ListView in android?