Search code examples
androidresizeexifbitmapfactory

Android: wrong orientation of files opened with BitmapFactory.decodeFile


Just simple as the title, files opened with BitmapFactory.decodeFile have wrong orientation when it is displayed on the ImageView. The image its captured from the camera and saved on a tmp file so if the device has the bug that returns data.getData() null I have at least a reference to the file.

This just start the camera activity and capture the image file

private void startCamera() {
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    if (hasImageCaptureBug()) {
        intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Constants.TMPFILE_PATH)));
    } else {
        intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    }
    startActivityForResult(intent, CAMERA_PIC_REQUEST);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_PIC_REQUEST) {
        if (resultCode == RESULT_OK) {
            Uri uri = null;

            if (hasImageCaptureBug()) {
                File f = new File(Constants.TMPFILE_PATH);
                try {
                    uri = Uri.parse(android.provider.MediaStore.Images.Media.insertImage(getContentResolver(), f.getAbsolutePath(), null, null));
                } catch (FileNotFoundException e) {

                }
            } else {
                uri = data.getData();
            }

            imageFilePath = Image.getPath(this, uri);

            if (Image.exists(imageFilePath)) {
                ImageView image = (ImageView) findViewById(R.id.thumbnail);
                int targetW     = (int) getResources().getDimension(R.dimen.thumbnail_screen_width);
                int degrees     = (int) Image.getRotation(this, uri);

                Bitmap bmp = Image.resize(imageFilePath, targetW);
                bmp = Image.rotate(bmp, degrees);

                image.setAdjustViewBounds(true);
                image.setImageBitmap(bmp);
            }
        }
    }
}

And this file resizes the image

public class Image {
    public static Bitmap resize(String pathName, int targetW) {
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;

        Bitmap bmp = BitmapFactory.decodeFile(pathName, opts);

        int photoW = opts.outWidth;
        int photoH = opts.outHeight;
        int targetH = Math.round((photoH * targetW) / photoW);
        int scaleFactor = Math.min(photoW/targetW, photoH/targetH);

        opts.inJustDecodeBounds = false;
        opts.inSampleSize = scaleFactor;
        opts.inPurgeable = true;

        bmp = BitmapFactory.decodeFile(pathName, opts);

        return bmp;
    }
}

Tryed to get the ExifOrientation but always its 0 because the file itself its correctly oriented just when I load it the file is displayed with the wrong orientation.

Regards


Solution

  • seems that my issue to preview the image was the Constants.TMPFILE_PATH, the image was not saved there, I just use this fix Display the latest picture taken in the image view layout in android!, but the issue persist if I post it to the server... I'll check this as answered and open a new question to this...


    Edited

    To solve this issue just refactor the new image and then upload it to the server, because the raw data of the file itself has his exif orientation was wrong.