Friday, August 31, 2012

MME USING SEND IMAGE OR FILE TO WEB SERVER IN ANDROID

hi friends Today see to how to upload a image and file to web server using method

I'm discuss with few technique and method are:

1. mime method
2.content type method
3.ksoap method
4.mime with json .


mime :

if its using to add jar file download jar file link

download to the binary file.



public class UploadActivity extends Activity {
    /** Called when the activity is first created. */
Bitmap bm;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        try {
//bm = BitmapFactory.decodeResource(getResources(),
//R.drawable.forest);
bm = BitmapFactory.decodeFile("/sdcard/DCIM/forest.png");
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("your server link");

ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg");
// File file= new File("/mnt/sdcard/forest.png");
// FileBody bin = new FileBody(file);
MultipartEntity reqEntity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("uploaded", bab);
reqEntity.addPart("photoCaption", new StringBody("sfsdfsdf"));
postRequest.setEntity(reqEntity);
HttpResponse response = httpClient.execute(postRequest);
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
String sResponse;
StringBuilder s = new StringBuilder();

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


you just create the project and copy the code and paste and add to library file. Its working.


if you want to download a mine with json project just :http://vikaskanani.wordpress.com/2011/01/29/android-image-upload-activity/

No comments:

Post a Comment