为什么在我的Andr​​oid应用程序的图像选择器自动旋转纵向图像转换成风景? [英] Why does the Image Picker in my Android app rotate automatically portrait images into landscape?

查看:213
本文介绍了为什么在我的Andr​​oid应用程序的图像选择器自动旋转纵向图像转换成风景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用这个code在我的Andr​​oid应用程序启动相机/画廊获取图像,并将其显示到ImageView的。

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

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

问题

这是我的code:

 私人无效openImageIntent(){
        //摄像头。
        System.gc()的;        最终名单<&意向GT; cameraIntents =新的ArrayList<&意向GT;();
        最终意图captureIntent =新意图(android.provider.MediaStore.ACTION_IM​​AGE_CAPTURE);
        最终的软件包管理系统软件包管理系统= getPackageManager();
        最终名单< ResolveInfo> listCam = packageManager.queryIntentActivities(captureIntent,0);
        对于(ResolveInfo RES:listCam){
            最后弦乐的packageName = res.activityInfo.packageName;
            最终意向意图=新意图(captureIntent);
            intent.setComponent(新组件名(res.activityInfo.packageName,res.activityInfo.name));
            intent.setPackage(的packageName);
            cameraIntents.add(意向);
        }        // 文件系统。
        最终意图galleryIntent =新意图();
        galleryIntent.setType(图像/ *);
        galleryIntent.setAction(Intent.ACTION_GET_CONTENT);        //文件系统的选项选择器。
        意向chooserIntent = Intent.createChooser(galleryIntentScegli鸽子prelevare L'想象,);        //添加摄像头的选项。
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,cameraIntents.toArray(新Parcelable [] {}));
        //Log.i(\"sto lanciando金正日选择器,瓦多EH);
        startActivityForResult(chooserIntent,4982);
    }
    私人字符串selectedImagePath;
    @覆盖
    保护无效的onActivityResult(INT申请code,INT结果code,意图数据){
        super.onActivityResult(要求code,结果code,数据);        如果(结果code == RESULT_OK){
            如果(要求code == 4982){
                乌里selectedImageUri = data.getData();
                selectedImagePath =的getPath(selectedImageUri);
                //Log.i(\"immagine\",selectedImagePath);                myImageView.setImageBitmap(德codeSampledBitmapFromFile(新文件(selectedImagePath)));            }
        }    }
    公共字符串的getPath(URI URI){
        的String [] =投影{MediaStore.Images.Media.DATA};
        光标光标= managedQuery(URI,投影,NULL,NULL,NULL);
        INT与Column_Index =光标
                .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        返回cursor.getString(Column_Index中);
    }
    公共静态位图德codeSampledBitmapFromFile(档案文件){        //首先去code。与inJustDe codeBounds = true来检查尺寸
         BitmapFactory.Options O =新BitmapFactory.Options();
            o.inJustDe codeBounds = TRUE;
        BitmapFactory.de codeFILE(file.getAbsolutePath(),O);        //我们希望新的大小缩放到
        最终诠释REQUIRED_SIZE = 430;        //找到正确的比例值。它应该是2的幂。
        int标= 1;
        而(o.outWidth /规模/ 2 - = REQUIRED_SIZE和放大器;&安培; o.outHeight /规模/ 2 - = REQUIRED_SIZE)
            规模* = 2;        //德code。与inSampleSize
        BitmapFactory.Options O2 =新BitmapFactory.Options();
        o2.inSampleSize =规模;        返回BitmapFactory.de codeFILE(file.getAbsolutePath(),O2);
       }


解决方案

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

I'm using this code in my android app to launch camera/gallery to get an Image, and display it into an ImageView.

When the user picks an image with landscape orientation everythings works fine, but when the user picks a portrait image, the image is displayed rotated by 90 degrees. I can't understand why..I'm testing my app on a Galaxy S3 with Android 4.3

I've noticed that the problems only occours when the picture is taken by the phone..maybe this is a problem with the S3?

This is my code:

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

解决方案

Many Android devices, if the device is held in portrait when a picture is taken, do not rotate the image to be portrait. Rather, they store the image in landscape, and put an EXIF header in the image to tell any image viewer "hey! please rotate this 270 degrees!". Most likely, that is what you are encountering, as I know that Samsung devices often have this issue.

这篇关于为什么在我的Andr​​oid应用程序的图像选择器自动旋转纵向图像转换成风景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆