1

This question is borrowed from the same question, beacuse i faced same problem.During image posting in server side, image details could not getting in server side. Like this:

Failed to post image file information php server, userid ok but image file information could not post.In this case, Image save successfully on server, i could not get information on php sever of image.

Here is the Code:

public class UploadImageHttpPost {

String testpath="/mnt/sdcard/14111.jpg";
String url = "http://andtuts.com/uploadImage.php";

public static void sendPost(String url, String imagePath,String userId)
        throws IOException, ClientProtocolException {
     String responseBody;

    MultipartEntity entity = new MultipartEntity(
            HttpMultipartMode.BROWSER_COMPATIBLE);

    File file = new File(imagePath);
    FileBody encFile = new FileBody(file,"image/jpeg");
    entity.addPart("images", encFile);
    entity.addPart("UserId", new StringBody(userId));
    HttpPost request = new HttpPost(url);
    request.setEntity(entity);

    HttpClient client = new DefaultHttpClient();

    ResponseHandler<String> responsehandler = new BasicResponseHandler();
    responseBody = client.execute(request, responsehandler);

    if (responseBody != null && responseBody.length() > 0) {
        Log.w("TAG", "Response from image upload->" + responseBody);

    }
}

I get Like this:

Array
(
    [UserId] => 1
)

where i using this in php for testing , what cames from multipart:

$filedata = $_FILES['images'];
$f = fopen(time().".txt",'wb');
fwrite($f, print_r($_REQUEST,true));
fclose($f);

But here below part missing means i am not getting, is there any missing code in multipart post method please check above java code:

[images] => Array
        (
            [name] => ball.png
            [type] => image/png
            [tmp_name] => /tmp/phpiQHIXQ
            [error] => 0
            [size] => 1664972
        )

I following this tutorials also: [http://www.webspeaks.in/2012/08/upload-files-from-android-to-server.html][2]

[2]: http://www.webspeaks.in/2012/08/upload-files-from-android-to-server.html and lots of other from the googled, but could not find proper solution.

Community
  • 1
  • 1
No_Name_Code
  • 299
  • 10
  • 20

1 Answers1

4

I recently doing just like your problem Here is the Solution:

Image take from sdcard and camera:

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Bitmap scaledphoto = null;
        int height = 100;
        int width = 100;
        if (resultCode == RESULT_OK) {
            if (requestCode == SD_REQUEST) {
                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
                yourSelectedImage = BitmapFactory.decodeFile(selectedImagePath);
                scaledphoto = Bitmap.createScaledBitmap(yourSelectedImage,
                        height, width, true);
                pImage.setImageBitmap(scaledphoto);

                Uri selectedImageUri1 = data.getData();
                path = getRealPathFromURI(selectedImageUri1);


            }
            if (requestCode == CAMERA_REQUEST) {
                yourSelectedImage = (Bitmap) data.getExtras().get("data");
                scaledphoto = Bitmap.createScaledBitmap(yourSelectedImage,
                        height, width, true);
                profImage.setImageBitmap(scaledphoto);
                Uri selectedImageUri1 = data.getData();
                path = getRealPathFromURI(selectedImageUri1);

            }
        }

Now you call the Function, where you want to upload pictures:

    String url="http://test.com/uploadimage.php";
    //path is the defined, which is take from camera and sdcard.
   // userid is, which user do you want upload picture.
    UploadImageHttp.sendPost(url, path, userId);

Here is the Class of Upload Image:

public class UploadImageHttp {
public static void sendPost(String url, String imagePath,String userId)
        throws IOException, ClientProtocolException {

    String responseBody;
    HttpClient client = new DefaultHttpClient();
    HttpPost request = new HttpPost(url);

    MultipartEntity entity = new MultipartEntity(
            HttpMultipartMode.BROWSER_COMPATIBLE);

    File file = new File(imagePath);
    ContentBody encFile = new FileBody(file,"image/png");

    entity.addPart("images", encFile);
    entity.addPart("UserId", new StringBody(userId));

    request.setEntity(entity);



    ResponseHandler<String> responsehandler = new BasicResponseHandler();
    responseBody = client.execute(request, responsehandler);


    if (responseBody != null && responseBody.length() > 0) {
        Log.w("TAG", "Response image upload" + responseBody);

    }
}

I hope you satisfied for this code.I run successfully.

ichigolas
  • 7,595
  • 27
  • 50
Horrorgoogle
  • 7,858
  • 11
  • 48
  • 81
  • Thanks for perfect solution. you can download HttpClient from the link below. http://mirror.metrocast.net/apache//httpcomponents/httpclient/source/httpcomponents-client-4.2.5-src.zip – Govind Totla Jul 18 '13 at 13:44