3

I have been uploading images to FirebaseStorage, but often they are the wrong way around when displaying them. I have discovered the ExifInterface that can determined the orientation of the image and rotate and flip it if necessary.

When selecting the image from the gallery area on my phone I get this error.

FileNotFoundError

I can select the image on my phone from the gallery and It can be displayed on the page.

The differences between the URI address and the data is one /

Data.getData() address : content://media/external/images/media/53331

uri.toString() address: content:/media/external/images/media/53331

I'm using the uri address as the images absolute path of the image to be able to rotate it if necessary. I pass this value into another method called modifyOrientation which then rotates it. Once it is passed into the method it reaches the line

ExifInterface ei = new ExifInterface(image_absolute_path);

and then returns to the catch as the file was not found.

Below is the entire error I'm getting as well as all my code. How can I fix this issue that I'm having. So when I pass the URI across into the next method it actually has the correct address.

Entire error

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);
        final FirebaseUser user = auth.getCurrentUser();

        if (requestCode == GALLERY_INTENT && resultCode == RESULT_OK)
        {
            progressDialog = new ProgressDialog(getActivity());
            progressDialog.setMessage("Displaying Image...");
            progressDialog.show();

            //imageUri = data.getData();
            //Picasso.get().load(imageUri).into(profileImage);


            final Uri uri = data.getData();


            File file = new File(uri.toString());
            file.getAbsolutePath();

            progressDialog.dismiss();
            try
            {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri);
                modifyOrientation(bitmap,file.getAbsolutePath());
                profileImage.setImageBitmap(bitmap);
            }
            catch(IOException e)
            {
                e.getStackTrace();
            }
        }
    }

    public static Bitmap modifyOrientation(Bitmap bitmap, String image_absolute_path) throws IOException {
        ExifInterface ei = new ExifInterface(image_absolute_path);
        int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                return rotate(bitmap, 90);

            case ExifInterface.ORIENTATION_ROTATE_180:
                return rotate(bitmap, 180);

            case ExifInterface.ORIENTATION_ROTATE_270:
                return rotate(bitmap, 270);

            case ExifInterface.ORIENTATION_FLIP_HORIZONTAL:
                return flip(bitmap, true, false);

            case ExifInterface.ORIENTATION_FLIP_VERTICAL:
                return flip(bitmap, false, true);

            default:
                return bitmap;
        }
    }

    public static Bitmap rotate(Bitmap bitmap, float degrees) {
        Matrix matrix = new Matrix();
        matrix.postRotate(degrees);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }

    public static Bitmap flip(Bitmap bitmap, boolean horizontal, boolean vertical) {
        Matrix matrix = new Matrix();
        matrix.preScale(horizontal ? -1 : 1, vertical ? -1 : 1);
        return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }

1 Answer 1

20

A Uri is not a File.

Step #1: Delete File file = new File(uri.toString());

Step #2: Make sure that you are using the Support Library edition of ExifInterface

Step #3: Call getContentResolver() on your Activity to get a ContentResolver

Step #4: Call openInputStream() on the ContentResolver, passing in the Uri, to get an InputStream on the content pointed to by that Uri

Step #5: Pass that InputStream to the ExifInterface constructor

Step #6: Use that ExifInterface as you are presently, to determine the image orientation

Step #7: Once you get things working, move all of this I/O to a background thread

1
  • 2
    My error was that I was using passing the Uri to the ExifInterface constructor. Create an InputStream from the Uri was the solution. contentResolver.openInputStream(uri) Mar 3, 2022 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.