0

My Problem here is while uploading image to the Server, the image uploads rotated. Any Help Please. Can i get any help in rectifying my problem. Help me out. And i don't know how to use exif interface to do that.

Uri imagesUri = null;

if (imagesUri != null) {
setApicall();
Uri myUri = Uri.parse(imagesUri.toString());
getApicall(myUri, "My Image");
} else {
showAlert("Attention..", "Please Insert Image.");
}

public void onActivityResult(int requestCode, int resultcode, Intent data) {
    super.onActivityResult(requestCode, resultcode, data);
    if (resultcode == RESULT_OK && requestCode == PICK_IMAGE) {
        tv_add_photo.setVisibility(View.GONE);
        imagesUri = data.getData();
        image.setImageURI(imagesUri);
    }
}

private String getRealPathFromURI(Uri contentUri) {
    String[] proj = {MediaStore.Images.Media.DATA};
    CursorLoader loader = new CursorLoader(this, contentUri, proj, null, null, null);
    Cursor cursor = loader.loadInBackground();
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    String result = cursor.getString(column_index);
    cursor.close();
    return result;
}

private void getApicall(Uri fileUri, String desc) {
    try {
        File file = new File(getRealPathFromURI(fileUri));
        //creating request body for file
        RequestBody requestFile = RequestBody.create(MediaType.parse(getContentResolver().getType(fileUri)), file);
        RequestBody descBody = RequestBody.create(MediaType.parse("text/plain"), desc);
        Log.d("Key", getContentResolver().getType(fileUri));
        Call<ImageUploadResponse> getPhotoUpload = apiInterface.uploadImage(requestFile, descBody);
        getPhotoUpload.enqueue(ImageUploadResposeCallback);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
  • Just to be curious, Are you using a Samsung phone? Because I had the same problem on this smartphones – Aspicas Jan 09 '20 at 12:57
  • Pixel 2 XL, android 10 OS. – Ganesh Kumar Jan 09 '20 at 12:58
  • 1
    It depends on the manufacture of a device. Please check https://stackoverflow.com/questions/14066038/why-does-an-image-captured-using-camera-intent-gets-rotated-on-some-devices-on-a – Alexander Jan 09 '20 at 13:13
  • Okay @GaneshKumar it depends of the brand but... to solve, you need to rotate programatically then upload to server. The problem is the device, not your code. – Aspicas Jan 09 '20 at 13:26
  • `apiInterface.uploadImage(requestFile........`. Well what happens in that function? You did not post the relevant upload code that does it. But... if you would just upload the image file and not mess around with bitmaps nothing would get rotated. – blackapps Jan 09 '20 at 17:16

1 Answers1

1

Try below method to rotate your image in different types of the ratio bypassing path of image

public static int getImageAngle(String picturePath) {
        try {
            ExifInterface ei = new ExifInterface(picturePath);
            int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    Log.i("TEST", "orientation : " + 90);
                    return 90;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    Log.i("TEST", "orientation : " + 180);
                    return 180;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    Log.i("TEST", "orientation : " + 270);
                    return 270;
                default:
                    Log.i("TEST", "orientation : " + 0);
                    return 0;
            }

        } catch (IOException e) {
            //Log.e("TEST", "" + e.getMessage());
            return 0;
        }
    }

    public static Bitmap rotateImage(Bitmap source, float angle) {
        Bitmap retVal;

        Matrix matrix = new Matrix();
        matrix.postRotate(angle);
        retVal = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
    return retVal;
}
Kalpesh Rupani
  • 991
  • 4
  • 12