3

I'm working on a Android-Project, in which the User can upload a Image as Profile image. It all worked fine until some point (eventually a Samsung-Update). Since then, the Images on Samsung Galaxy devices (maybe also on others), but on my HTC One XL it works fine, on my Lenovo Yoga Tablet also and on a Sony (don't know exactly which) also. But on the Galaxy's S6 and S5 it rotates the Image. The Idea is, that an Image is set in the "normal" perspective as an User took it. It means, It should take just a square, but of course, that the had is upright. With the Samsung-Devices the Head is 90 degree wrong anticlockwise. The Code works perfectly on other Devices. Has someone the same Problem? Any Idea? Here is some Code

// After image taken
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    try {
        // data returned
        if (resultCode != FragmentActivity.RESULT_CANCELED) {
            if (requestCode == RESULT_LOAD_IMAGE && resultCode == getActivity().RESULT_OK){
                // Set profile image from gallery
                try {

                    Uri selectedImage = data.getData();
                    photo = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), selectedImage);
                    photo = MainUtils.rotateBitmap(photo, data, getActivity());

                    photo = MainUtils.resizeBitmapIfNeeded(photo, 800, 800);

                    byte[] byteArray;
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();
                    photo.compress(Bitmap.CompressFormat.JPEG, 90, stream);
                    byteArray = stream.toByteArray();

                    // Save image to parse as ParseFile and connect to the ParseUser (redacted)
            }

MainUtils Methods:

public static Bitmap resizeBitmapIfNeeded(Bitmap image, int maxWidth, int maxHeight) {
    if (maxHeight > 0 && maxWidth > 0) {

        int wid = image.getWidth();
        int hei = image.getHeight();

        MainUtils.log(" resizeBitmapIfNeeded, size is " + wid + " X " + hei);

        if (wid > maxWidth || hei > maxHeight) {
            int width = image.getWidth();
            int height = image.getHeight();
            float ratioBitmap = (float) width / (float) height;
            float ratioMax = (float) maxWidth / (float) maxHeight;

            int finalWidth = maxWidth;
            int finalHeight = maxHeight;
            if (ratioMax > 1) {
                finalWidth = (int) ((float) maxHeight * ratioBitmap);
            } else {
                finalHeight = (int) ((float) maxWidth / ratioBitmap);
            }
            image = Bitmap.createScaledBitmap(image, finalWidth, finalHeight, true);

            wid = image.getWidth();
            hei = image.getHeight();
            MainUtils.log(" resizeBitmapIfNeeded, resized size is " + wid + " X " + hei);
            return image;
        } else {
            return image;
        }
    } else {
        return image;
    }
}

public static Bitmap rotateBitmap(Bitmap bitmap, Intent data, Context context) {

    int orientation = 0;

    try {
        Uri selectedImage = data.getData();
        ExifInterface exif;
        exif = new ExifInterface(MainUtils.getRealPathFromUri(context, selectedImage));
        orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_UNDEFINED);

        MainUtils.log("Orientation is " + orientation);

    } catch (IOException excep) {
        MainUtils.log("Rotate " + excep.getMessage());
    }

    Matrix matrix = new Matrix();
    switch (orientation) {
        case ExifInterface.ORIENTATION_NORMAL:
            return bitmap;
        case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
            matrix.setScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            matrix.postRotate(180);
            break;
        case ExifInterface.ORIENTATION_FLIP_VERTICAL:
            matrix.postRotate(180);
            matrix.postScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_TRANSPOSE:
            matrix.postRotate(90);
            matrix.postScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            matrix.postRotate(90);
            break;
        case ExifInterface.ORIENTATION_TRANSVERSE:
            matrix.postRotate(-90);
            matrix.postScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            matrix.postRotate(-90);
            break;
        default:
            return bitmap;
    }
    try {
        Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        bitmap.recycle();
        return bmRotated;
    }
    catch (OutOfMemoryError e) {
        e.printStackTrace();
        return null;
    }
}

public static Bitmap rotateBitmapFromFile(String picturePath) {

    int orientation = 0;
    Bitmap bitmap = BitmapFactory.decodeFile(picturePath);

    try {
        ExifInterface exif = new ExifInterface(picturePath);
        orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

        MainUtils.log("Orientation is " + orientation);

    } catch (IOException excep) {
        MainUtils.log("Rotate " + excep.getMessage());
    }

    Matrix matrix = new Matrix();
    switch (orientation) {
        case ExifInterface.ORIENTATION_NORMAL:
            return bitmap;
        case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
            matrix.setScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            matrix.postRotate(180);
            break;
        case ExifInterface.ORIENTATION_FLIP_VERTICAL:
            matrix.postRotate(180);
            matrix.postScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_TRANSPOSE:
            matrix.postRotate(90);
            matrix.postScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            matrix.postRotate(90);
            break;
        case ExifInterface.ORIENTATION_TRANSVERSE:
            matrix.postRotate(-90);
            matrix.postScale(-1, 1);
            break;
        case ExifInterface.ORIENTATION_ROTATE_270:
            matrix.postRotate(-90);
            break;
        default:
            return bitmap;
    }
    try {
        Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
        bitmap.recycle();
        return bmRotated;
    }
    catch (OutOfMemoryError e) {
        e.printStackTrace();
        return null;
    }
}

Gallery Intent

Intent intent = new Intent(
                        Intent.ACTION_PICK,
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                intent.setType("image/*");
                startActivityForResult(
                        Intent.createChooser(intent, "Select File"),
                        galleryRequest);
Stephan Boner
  • 733
  • 1
  • 6
  • 27
  • Why two requestCode's ? Please stick to one. Post the used intent at the start of the code. Show what you do with `mCurrentPhotoPath`. – greenapps Oct 26 '16 at 09:29
  • `it isn't possible to upload a Image, directly made on the Camera`. ????? – greenapps Oct 26 '16 at 09:31
  • I asked you to focus your problem to only one. So one intent only. And one requestCode. Choose for camera or gallery. Adapt your post. I will wait. – greenapps Oct 26 '16 at 09:56
  • @greenapps Thanks, I've edited it. – Stephan Boner Oct 26 '16 at 11:36
  • You call rotateBitmap() and then you complain that the image gets rotated? – greenapps Oct 26 '16 at 13:34
  • And you did not tell which orientation comes to play. Which case of that switch statement? Please tell exactly what happens. How could we ever know? – greenapps Oct 26 '16 at 13:35
  • @greenapps I've edited my Post. rotateBitmap should correct the Orientation Errors, which works on my other devices.. – Stephan Boner Oct 26 '16 at 19:19
  • You only repeat yourself. We already knew. But you do not give the info i asked for. You better answer my questions otherwise you cannot be helped. – greenapps Oct 26 '16 at 20:35
  • @greenapps Thats my biggest Problem - I have no Device which is affected, so I can't debug. So I don't know, which switch statement etc. I hoped somebody has the same error... – Stephan Boner Oct 26 '16 at 22:11
  • I dont have one either. Sorry. One culprit is your use of the ExifInterface class which requires a file path. So you are using `getRealPathFromUri()`. The use of such functions is cursed. They can return `null`. You could at least adapt your code in such a way that it checks for null before use. Then display a toast. – greenapps Oct 27 '16 at 10:41
  • Please do a test to see if `exif = new Exifinterface(null)` will cause an exception. If it is allowed then orientation will always be 0. – greenapps Oct 27 '16 at 10:44
  • Thank you very much for your effort @greenapps The Project is now by someone who knows Android very good. I'll ask him to post the answer here/I'll post the answer myself if it's solved. – Stephan Boner Oct 28 '16 at 08:32
  • 1
    Duplicate question. See answer of http://stackoverflow.com/questions/13511356/android-image-selected-from-gallery-orientation-is-always-0-exif-tag – myfknoll Oct 28 '16 at 12:23

1 Answers1

1

Duplicate Question, see Android image selected from gallery Orientation is always 0 : Exif TAG

Check answer of MKJParekh. What you have to do: 1.) Retrieve orientation of the bitmap media store 2.) Rotate the bitmap according the orientation if necessary

Community
  • 1
  • 1
myfknoll
  • 455
  • 5
  • 15