Search code examples
javaandroidimageviewsamsung-mobileonactivityresult

Photo orientation in imageView with Samsung mobile phone


I have a problem with the image on Samsung device Each time I take a photograph the vertical form but when it calls an image of the gallery the classroom Vertically It programmed with class ExifInterface and I can not find result

Every time I take a picture I call my class PreviewImage which is where I show the image Subsequently I have the button that takes me to assign the profile photo I have this method onActivityResult

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == Activity.RESULT_OK) {
        try {
            ExifInterface exifInterface = new ExifInterface(photoFile.getAbsolutePath());
            int exif = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
            switch (exif) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    orientation = 90;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    orientation = 180;
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    orientation = 270;
                    break;
            }
        } catch (Exception e) {
            Log.d("tmessages", e.toString());
        }
        Intent intent = new Intent(this, PhotoViewer.class);
        intent.putExtra(EXTRA_PHOTO_PATH, photoFile.getAbsolutePath());
        intent.putExtra(EXTRA_PHOTO_ORIENTATION, orientation);
        intent.putExtra(EXTRA_PHOTO_EDIT, false);
        startActivityForResult(intent, 10);

    } 

And this is my PhotoViewer class

   private void sendPicture() {
        Intent returnIntent = new Intent();  
        returnIntent.putExtra("photoDescription", photoDescription.getText().toString());
        returnIntent.putExtra("photoPath", localPath);
        returnIntent.putExtra("orientation", orientation);
        setResult(Activity.RESULT_OK, returnIntent);
        finish();
    }

Once the photo is taken it returns me to the onActivityResult method of the Profile class from where it was called

else if (requestCode == 10 && resultCode == Activity.RESULT_OK) {
        if (photoFile != null&&LoginController.getInstance().xmppConnection.isConnected()) {
            byte[] photo = AndroidUtilities
                    .createProfileImage(photoFile.getAbsolutePath());
            ProfileManager.getInstance().publishPhoto( photo);
            Bitmap decodedByte = BitmapFactory.decodeByteArray(photo, 0, photo.length);
            avatar.setImageBitmap(decodedByte);
        }else{
            Toast.makeText(this,R.string.LabelProfilePhotoFailed, Toast.LENGTH_SHORT).show();
        }
    }

How can I make the image show me vertical in the profile photo?


Solution

  • You have done half the job! Just use the orientation you calculated before :

    else if (requestCode == 10 && resultCode == Activity.RESULT_OK) {
        if (photoFile != null&&LoginController.getInstance().xmppConnection.isConnected()) {
            byte[] photo = AndroidUtilities
                    .createProfileImage(photoFile.getAbsolutePath());
            ProfileManager.getInstance().publishPhoto( photo);
            Bitmap decodedByte = BitmapFactory.decodeByteArray(photo, 0, photo.length);
     ->     decodedByte = rotate(decodedByte, orientation);
            avatar.setImageBitmap(decodedByte);
        }else{
            Toast.makeText(this,R.string.LabelProfilePhotoFailed, Toast.LENGTH_SHORT).show();
        }
    }
    

    When orientation = getIntent().getExtras().getInt(EXTRA_PHOTO_ORIENTATION).

    private static Bitmap rotate(Bitmap bm, int rotation) {
        if (rotation != 0) {
            Matrix matrix = new Matrix();
            matrix.postRotate(rotation);
            Bitmap bmOut = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(), matrix, true);
            return bmOut;
        }
        return bm;
    }