2

I want to send my file from android device to web api with the parameter of name, chunk, file data. I have this code: It doesn't return any error on me I don't know what I do wrong. Please help I'm spending much time with this and it still doesn't work.

FileBody localFileBody = new FileBody(new File("/storage/sdcard0/Pictures/Images/angel_productshot.jpg"),
                "image/jpg");

        MultipartEntity localMultipartEntity = new MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE);

        HttpParams httpParameters = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParameters, 15000);
        HttpConnectionParams.setSoTimeout(httpParameters, 15000);
        HttpPost httpPost = new HttpPost(
                "http://phdsafar4.myserver.com.ph:1217/api/fileupload");
        HttpClient httpclient = new DefaultHttpClient(httpParameters);
        httpPost.addHeader("Authorization","Basic " + Base64.encodeToString(
                        (username + ":" + password).getBytes(), Base64.NO_WRAP));

        httpPost.setHeader("Content-Type", "multipart/form-data");
        httpPost.setHeader("Accept", "application/image");
        localMultipartEntity.addPart("name", new StringBody(filename));
        localMultipartEntity.addPart("chunk", new StringBody("1"));
        localMultipartEntity.addPart("file data", localFileBody);
        httpPost.setEntity(localMultipartEntity);
        HttpResponse localHttpResponse = httpclient.execute(httpPost);
        Log.i("response upload", localHttpResponse.toString());
        Log.i("Multipart Entity", localMultipartEntity.toString());
2
  • Check stackoverflow.com/questions/4623507/… answer for uploading image to php web service. Commented Aug 6, 2013 at 5:52
  • Please tell me what's wrong on my code why it doesn't give an output. Commented Aug 6, 2013 at 6:01

2 Answers 2

4
this work for me try it...
 to use MultipartEntity must be add this two jar file in your project lib
 1. httpmime-4.0.1.jar
 2. apache-mime4j-0-.6.jar

MultipartEntity multipartEntity = new MultipartEntity();
File image1 = new File(imagePath);
multipartEntity.addPart("Image1",image1);
File image2 = new File(imagePath2);
multipartEntity.addPart("Image2",image2);
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(Url);
post.setEntity(multipartEntity);
HttpResponse response = client.execute(post);
HttpEntity resEntity = response.getEntity();
response_str = EntityUtils.toString(resEntity);
Sign up to request clarification or add additional context in comments.

1 Comment

I already have the httpmime-4.1.2.jar and ksoap2-android-assembly-2.5.8-jar-with-dependencies.jar
1

Go through following code

public static String uploadImage(Bitmap bitmap, String imageName, String imageUrl) {

    // String image_description = edtImageDescription.getText().toString();
    StringBuilder s = null;// = new StringBuilder();
    String sResponse;

    try {

        // ---------------------------------------------------------------------------------

        ByteArrayOutputStream bos = new ByteArrayOutputStream();

        bitmap.compress(CompressFormat.JPEG, 75, bos);

        byte[] data = bos.toByteArray();

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost(imageUrl);

        ByteArrayBody imageByte = new ByteArrayBody(data, imageName);

        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

        // sending a String param;


        entity.addPart("image_description","Sample_Image")

        entity.addPart("image_name", imageByte);

        postRequest.setEntity(entity);

        HttpResponse response = httpClient.execute(postRequest);
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));

        s = new StringBuilder();

        while ((sResponse = reader.readLine()) != null) {

            s = s.append(sResponse);

        }

        Logger.logger("Utilities", "Response: " + s);

    } catch (Exception e) {

        Logger.logger("Utilities", "Some error came up");

    }

    if (s != null) {
        return s.toString();
    } else {
        return null;
    }

}

2 Comments

why I need to get the bitmap I need the file not the bitmap because I'm using that code to send all files including txt files not only image
@NewDroidDev , for sending image to server you need to change it into byte array and byte array is created from bitmap.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.