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. Get body movement (e.g. turn around)

I'm working on an app that needs body movements as inputs. So
basically I want to know if a person turns around completely (360).
If a the person does sthg happens inside the application. My problem
for now is: How to figure out if a person turned around?
I don't see an approach with gps as the person is not moving enough.
So I thought about the compass or acceleration sensor but I have no
clue how to manage that. I'd really appreciate some ideas / hints /




2. SMS text garbled using native Messaging app when message spans two text messages?


I'm working with the sms intent. Testing this on 2.0 emulators.

If I use the built-in messaging app to create a text message which
spans two messages, the receiving emulator displays the received
message as garbled text. Looks like it's encoded or compressed etc.
Does anyone else see this? Is this just an issue with the emulators,
and won't happen on a real device? If I send a message < 160
characters, the other emulator gets it ok.



3. Debugging on mobile 2

4. Droid/Milestone: GL_OES_framebuffer_object reported but not working

5. AES decryption is slow....

6. Mohon gabung ..

7. twtpic4j is not throwing exception