如何在纵向模式下设置图像 从Android中的图库中进行选择后强制要求



我想在从图库中选择图像后在纵向中强制显示图像。我正在为它使用以下代码。

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);
                    }
                });

活动结果

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();
        }
在这里,我

每次旋转图像时都会得到图像,但如果图像处于横向模式,我只想要图像的纵向模式。

选择图像后,您必须强制将屏幕的方向更改为横向

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

这是检查屏幕是陆地还是端口

的代码
   Configuration newConfig = getResources().getConfiguration();
            if(newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){
//PORTRAIT
    }else{
//Land
    }

试试这个

 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();
 }

最新更新