从Android中的Gallery中选择后,如何在纵向模式下设置图像?

时间:2014-01-06 11:13:43

标签: android orientation

从Gallery中选择图像后,我想以纵向显示图像。我正在使用下面的代码。

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

//这里我每次旋转图像,但如果图像处于横向模式,我只想要图像的纵向模式。

2 个答案:

答案 0 :(得分:0)

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

setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

以下是检查屏幕是陆地还是端口的代码

   Configuration newConfig = getResources().getConfiguration();

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

答案 1 :(得分:0)

试试这个

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