1

I want to display image in compulsory in portrait after selecting image from Gallery. I am using below code for it.

findViewById(R.id.btn_open_galllery).setOnClickListener(
                new OnClickListener() {

                    @Override
                    public void onClick(View arg0) {

                        Intent selectPictureIntent = new Intent(
                                Intent.ACTION_PICK);
                        selectPictureIntent.setType("image/*");

                        startActivityForResult(selectPictureIntent, 1212);
                    }
                });

// On Activity Result

Uri selectedImageUri = data.getData();

        Bitmap bitmap = null;
        try {
            bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        File file = new File(selectedImageUri.getPath());

        try {
            ExifInterface exif = new ExifInterface(file.getAbsolutePath());
            orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
            Log.e("Orientation", ""+orientation);
            Matrix matrix = new Matrix();
            matrix.postRotate(90);
            Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
            iv.setImageBitmap(rotatedBitmap);
        } catch (IOException e1) {
            e1.printStackTrace();
        }

// Here i got every time rotation of image, but i want only portrait mode of image if image is in landscape mode.

3

2 Answers 2

0

After selecting the image,forcefully you have to change the orientation of the screen as landscape

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

Here is the code to check whether screen is land or port

   Configuration newConfig = getResources().getConfiguration();

            if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
//PORTRAIT
    }else{
//Land
    }
0

Try this

 Bitmap bitmap = null;
 try {
     bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);
     if(bitmap.getHeight()< bitmap.getWidth()){
         Canvas c = new Canvas(bitmap);
         c.rotate(270);
     }

     // Your rotated image is ready you can use it now

 } catch (FileNotFoundException e) {
     e.printStackTrace();
 } catch (IOException e) {
     e.printStackTrace();
 }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.