Question

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);
       }
Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top