2

I am using following code to upload image to server,it is successfully uploaded but image orientation changing with -90.

I didnt understand how to solve this issue. My image in Sdcard is in correct orientation but I didnt get to know why this image changes its orientation.

HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(image_upload);

Log.e("strImagePath.... before uploading", al_image_paths.get(i));


multipartContent.addPart("image", new FileBody(new File(al_image_paths.get(i))));

multipartContent.addPart("sellleadid", new StringBody("2234"));
multipartContent.addPart("action", new StringBody("Send Used Car Images"));
multipartContent.addPart("app_id", new StringBody("1"));

totalSize = multipartContent.getContentLength();

httpPost.setEntity(multipartContent);
HttpResponse response = httpClient.execute(httpPost, localContext);
String serverResponse = EntityUtils.toString(response.getEntity());
Log.e("serverResponse image", "<> " + serverResponse);
Piyin
  • 1,823
  • 1
  • 16
  • 23
veerendra
  • 523
  • 7
  • 18
  • 3
    Your problem is that your image data is not rotated—the image has EXIF orientation data set that indicates the rotation. Some viewers take the EXIF orientation into account when displaying the image, some don't. To save processing time and memory, most mobile devices always write the image data in one particular (normally landscape) orientation, and, if you were holding the camera a different way, just set this single small piece of EXIF metadata to indicate that, rather than rotating the data itself. – Matt Gibson Jul 08 '14 at 10:37
  • @MattGibson I think you can post this as an answer – ntv1000 Jul 08 '14 at 10:40
  • my image path shows the correct image in my phone,so how can I upload the same image to server without any tilt. – veerendra Jul 08 '14 at 10:40
  • @ntv1000 It might be the reason why, but it doesn't actually fix the problem. I'll leave that to someone else, though I'd imagine that there's already a existing answered question on "how do I rotate this image to match its EXIF"... – Matt Gibson Jul 08 '14 at 10:43
  • In the phone it will be correct. but while transferring to server it will be tilted. So, you need to tilt the image in mobile phone and transfer to server. Refer my answer for code. – ngrashia Jul 08 '14 at 10:44
  • @user1761316 In other words: You most likely took the photo with the wrong orientation and then rotated it in your Gallery, but this doesn't really rotate the image on the disk, it just renders it rotated. Now when you upload it to the server, you read the original photo from the disk, which has the original(in your case wrong) orientation. – ntv1000 Jul 08 '14 at 10:46
  • Another approach would be to rotate the image once it gets to the server—this is sometimes preferable, as the server has more memory and CPU time for doing the rotation, and you may already be processing the image on the server (to resize it for web display, for example.) You should make this choice based on the overall design of your app and the image sizes it's dealing with. – Matt Gibson Jul 08 '14 at 10:46
  • `While transferring to server the image will be tilted` well i don't believe that. Then who is responsible? FileBody? Http client? The server script? No nobody. The image is just a file. The file will be transported to the server byte by byte. Nothing changes. A server script normally receives the bytes and saves to file. It is still the same file. Only if the (web)server is going to show the image the used software can show it rotated. – greenapps Jul 08 '14 at 19:09

2 Answers2

4

There is unfortunately no way of modifying the orientation of the photo file other other than to load the image, rotate it manually and re-save it in it's correct orientation.

You can refer this code for image rotation samples:

int rotate = 0;
try {
    getContentResolver().notifyChange(imageUri, null);
    File imageFile = new File(al_image_paths.get(i)); 
    ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

    switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_270:
            rotate = 270;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            rotate = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotate = 90;
            break;
    }
    Log.v(Common.TAG, "Exif orientation: " + orientation);

    Bitmap rotattedBitmap= BitmapFactory.decodeFile(al_image_paths.get(i));           
    Matrix matrix = new Matrix();
    matrix.postRotate(rotate);
    return Bitmap.createBitmap(rotattedBitmap, 0, 0, rotattedBitmap.getWidth(), rotattedBitmap.getHeight(), matrix, true);
} catch (Exception e) {
    e.printStackTrace();
}

EDIT: In your case, you have to do following:

  1. Identify image EXIF orientation
  2. Create the bitmap
  3. Rotate the bitmap
  4. Save the bitmap as image
  5. Upload image to server
Piyin
  • 1,823
  • 1
  • 16
  • 23
ngrashia
  • 9,869
  • 5
  • 43
  • 58
  • @user1761316: Answer updated. You can also send the file directly to server and use the rotate option in your web-page via jquery or something if you are more comfortable that way. – ngrashia Jul 08 '14 at 11:00
  • I need to upload File format,so can I convert Bitmap to File.multipartContent.addPart("image", new FileBody(new File( al_image_paths.get(i)))) – veerendra Jul 08 '14 at 11:11
0

use that code

ExifInterface exif;
int angle = 0;
try {
    exif = new ExifInterface(myUri.getPath());
    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
    if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
        angle = 90;
    } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
        angle = 180;
    }
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
Matrix  matrix1 = new Matrix();

//set image rotation value to 45 degrees in matrix.
matrix1.postRotate(angle);

//Create bitmap with new values.
Bitmap photo = Bitmap.createBitmap( bitmap, 0, 0, bitmap.getWidth(),  bitmap.getHeight(), matrix1, true);
Piyin
  • 1,823
  • 1
  • 16
  • 23
The Ray of Hope
  • 738
  • 1
  • 6
  • 16