1

I am trying to achieve a image upload to a server from android

  • I have one image,one button
  • on click of button i need to post the images to the server
  • I need to use multiparty for this

my url which i need to post::http://54.218.73.244:7002/Details/


What i have tried ::

  • i have posted a similar question before here but i couldn't achieve mush success
  • I have implemented the server scripting and it accepts a image with the name key

drawable has image.jpg in it


activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="135dp"
        android:layout_height="181dp"
        android:layout_alignParentTop="true"
        android:layout_marginTop="32dp"
        android:clickable="false"
        android:src="@drawable/image" />

    <Button
        android:id="@+id/SUBMIT_BUTTON_ID"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="47dp"
        android:text="SUBMIT" />

</LinearLayout>

what i need::

  • How to write the java functionality for this ?
  • My attempt here using MyActivity.java was unsuccessful so looking for it from scratch

i am a newbie for android & image uploading, please be easy on answers

Community
  • 1
  • 1
smriti3
  • 871
  • 2
  • 15
  • 35

2 Answers2

0

I use Http multipart for performing image and video upload. Sadly these are not part of the android sdk, so we have to import the multipart lib as part of your project.

Here is an awesome answer that helped me.

Dont forget to cross check that the key-value pair that you are adding as multipart is exactly what the server is expecting.

Community
  • 1
  • 1
amalBit
  • 12,041
  • 6
  • 77
  • 94
0

Try this code..

   private void uploadPhoto(Bitmap photo)
    {
    MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

    try 
    {

        ContentBody contentPart = null;
        if(photo != null)
        {
            String filename="temp.jpg";
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            photo.compress(Bitmap.CompressFormat.JPEG, 100, bos);
            contentPart = new ByteArrayBody(bos.toByteArray(), filename);        

            String root = "sdcard path";
            File myDir = new File(root );    
            myDir.mkdirs();
            String fname = filename;
            File file = new File (myDir, fname);
            if (file.exists ()) file.delete (); 
            try {
                FileOutputStream out = new FileOutputStream(file);
                photo.compress(Bitmap.CompressFormat.JPEG, 90, out);
                out.flush();
                out.close();

            } catch (Exception e) {
                e.printStackTrace();
            }

            reqEntity.addPart("image", contentPart);
        }



        String url = "your url";
        String response = multipost(url, reqEntity);

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}

and here is code for multipost

   public String multipost(String urlString, MultipartEntity reqEntity) {


HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(urlString);
//
try {

    httppost.setEntity(reqEntity);
    HttpResponse response = httpclient.execute(httppost);
    InputStream is = response.getEntity().getContent();

}
catch(Exception e)
{
    Log.e("tag", "Error:  "+e.toString());
}

return null;        
}

then call uploadphoto method...when u want.... Hope this will help u...:)

ADT
  • 255
  • 1
  • 4
  • 14