Welcome to DU! The truly grassroots left-of-center political community where regular people, not algorithms, drive the discussions and set the standards. Join the community: Create a free account Support DU (and get rid of ads!): Become a Star Member Latest Breaking News Editorials & Other Articles General Discussion The DU Lounge All Forums Issue Forums Culture Forums Alliance Forums Region Forums Support Forums Help & Search

SirRevolutionary

(579 posts)
5. I got the upload to work with this code
Tue Dec 20, 2011, 12:04 PM
Dec 2011

Found it here http://vikaskanani.wordpress.com/2011/01/11/android-upload-image-or-file-using-http-post-multi-part/

I used a simple PHP file on the server and just barely tweaked the sample that blog post provided. Also, he mentions you need the HttpClient 4.1-Beta1 jar (beta at the time), but you'll need to add the httpclient and httpmime jars to your Android project.

package com.test.testupload;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ByteArrayBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;

public class TestUploadActivity extends Activity {
Bitmap bm;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try {
bm = BitmapFactory.decodeFile("/sdcard/DCIM/100MEDIA/FOO.jpg&quot ;
executeMultipartPost();
} catch (Exception e) {
Log.e(e.getClass().getName(), e.getMessage());
}
}

public void executeMultipartPost() throws Exception {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bm.compress(CompressFormat.JPEG, 75, bos);
byte[] data = bos.toByteArray();
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(
"http://myserver.com/testUpload.php&quot ;
ByteArrayBody bab = new ByteArrayBody(data, "FOO.jpg&quot ;

MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("ufile", bab); // this is the PHP post var
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent(), "UTF-8&quot );
String sResponse;
StringBuilder s = new StringBuilder();

while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}
System.out.println("Response: " + s); // PHP echoes ok or fail
} catch (Exception e) {
// handle exception here
Log.e(e.getClass().getName(), e.getMessage());
}
}
}

This was a fun exercise I don't know how to remove the emoticons from the code in a DU post, pretty ironic for a developer, no?

Recommendations

0 members have recommended this reply (displayed in chronological order):

Latest Discussions»Retired Forums»Website, DB, & Software Developers»Android code instructions...»Reply #5