Anu
Anu

Reputation: 7177

Upload images to server in android?

In my android application, i am uploading images to the server(C#) via http post and its successfully uploaded to the server.

Hear is the code for image upload:

public static String uploadImagePost(String url, String imagePath) throws Exception  {

        HttpClient  httpClient      =   new DefaultHttpClient();
        HttpContext localContext    =   new BasicHttpContext();
        HttpPost    httpPost        =   new HttpPost(url);
        // add header 
        httpPost.addHeader("FileName",      "android"+System.currentTimeMillis()+".jpg");
        httpPost.setHeader("User-Agent",    "android_client");
        httpPost.addHeader("accept",        "application/json");

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();        
        /* example for setting a HttpMultipartMode */
        builder.setMode(HttpMultipartMode.RFC6532);
        /* example for adding an image part */
        builder.addPart("image/jpeg", new FileBody(new File (imagePath))); 
        httpPost.setEntity(builder.build());

        HttpResponse response = httpClient.execute(httpPost, localContext);
        System.out.println("Response Code : "   + response.getStatusLine().getStatusCode());
        BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

        StringBuffer result = new StringBuffer();
        String line = "";
        while ((line = rd.readLine()) != null) {
            result.append(line);
        }
        return result.toString();
 }

So my problem is the image witch i have uploaded is not readable. then i have check the source of the image using editor so i have noticed following text lines are included in the image source.

Header:

--5VkWFM_KqrrWu4l-DOUIyJabHz5tZ5_jp
Content-Disposition: form-data; name="image/jpeg"; filename="tempimage.jpg"
Content-Type: application/octet-stream

Footer:

 --5VkWFM_KqrrWu4l-DOUIyJabHz5tZ5_jp

After removing of above header and footer and save the image so image is now readable.So the problem is header and footer. How can i fixed this issue?

Upvotes: 3

Views: 4616

Answers (1)

Sanu
Sanu

Reputation: 455

Image decoding

                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                BitmapDrawable drawable = (BitmapDrawable) userPhoto.getDrawable();                     
                yourSelectedImage= drawable.getBitmap();    
                yourSelectedImage.compress(Bitmap.CompressFormat.PNG,100,baos);
                imageData = baos.toByteArray(); 

Send image using Httppost

  public static HttpResponse sendImage(byte[] image) {
    HttpResponse responsePOST = null;
    try {
        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(postURL);
        post.setHeader("Accept", "application/json");                               
        ByteArrayBody bab = new ByteArrayBody(image, firstname+".png");         
        MultipartEntity reqEntity = new MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE);          
        reqEntity.addPart("user_profile[image_attributes[attachment]]", bab);             
        post.setEntity(reqEntity);                      
        responsePOST = client.execute(post);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return responsePOST;
}

Upvotes: 1

Related Questions