-->

为什么在我的Android应用程序的图片选择器自动旋转肖像图片为景观?(Why does the I

2019-10-19 07:03发布

我用我的Android应用程序的代码来启动相机/画廊获得的图像,并显示到ImageView的。

当用户与横向万物选取图像工作正常,但是当用户选择一个人像图像,显示图像旋转90度。 我无法理解why..I'm搭载Android 4.3测试上的Galaxy S3我的应用程序

我注意到,当拍照由phone..maybe这是与S3的问题问题只有occours?

这是我的代码:

    private void openImageIntent() {
        // Camera.
        System.gc();

        final List<Intent> cameraIntents = new ArrayList<Intent>();
        final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        final PackageManager packageManager = getPackageManager();
        final List<ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent, 0);
        for(ResolveInfo res : listCam) {
            final String packageName = res.activityInfo.packageName;
            final Intent intent = new Intent(captureIntent);
            intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
            intent.setPackage(packageName);
            cameraIntents.add(intent);
        }

        // Filesystem.
        final Intent galleryIntent = new Intent();
        galleryIntent.setType("image/*");
        galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

        // Chooser of filesystem options.
        Intent chooserIntent = Intent.createChooser(galleryIntent, "Scegli dove prelevare l'immagine");

        // Add the camera options.
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[]{}));
        //Log.i("sto lanciando il chooser","vado eh");
        startActivityForResult(chooserIntent, 4982);
    }


    private String selectedImagePath;
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        super.onActivityResult(requestCode, resultCode, data);



        if (resultCode == RESULT_OK) {
            if (requestCode == 4982) {


                Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
                //Log.i("immagine",selectedImagePath);

                myImageView.setImageBitmap(decodeSampledBitmapFromFile(new File(selectedImagePath)));

            }
        }

    } 
    public String getPath(Uri uri) {
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        int column_index = cursor
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }


    public static Bitmap decodeSampledBitmapFromFile(File file) {

        // First decode with inJustDecodeBounds=true to check dimensions
         BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(file.getAbsolutePath(), o);

        //The new size we want to scale to
        final int REQUIRED_SIZE=430;

        //Find the correct scale value. It should be the power of 2.
        int scale=1;
        while(o.outWidth/scale/2>=REQUIRED_SIZE && o.outHeight/scale/2>=REQUIRED_SIZE)
            scale*=2;

        //Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize=scale;

        return BitmapFactory.decodeFile(file.getAbsolutePath(), o2);
       }

Answer 1:

许多Android设备,如果设备在纵向举行在拍摄照片时,不旋转图像变为纵向。 相反,他们的图像存储在景观,并把一个EXIF头图像中告诉任何图像浏览器“哎!请这个旋转270度!”。 最有可能的,那是你遇到了什么,因为我知道,三星的设备通常有这个问题。



文章来源: Why does the Image Picker in my Android app rotate automatically portrait images into landscape?