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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Hiren Patel
  • 52,124
  • 21
  • 173
  • 151

2 Answers2

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
    }
Amith
  • 1,907
  • 5
  • 29
  • 48
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();
 }
dinesh sharma
  • 3,312
  • 1
  • 22
  • 32