FileNotFoundException when Using ExifInterface

牧云@^-^@ 提交于 2020-01-11 11:33:14

问题


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.

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.

@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:


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



来源:https://stackoverflow.com/questions/49407931/filenotfoundexception-when-using-exifinterface

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!