使用ExifInterface时的FileNotFoundException

jav*_*123 1 java android exif rotation filenotfoundexception

我一直在将图像上传到FirebaseStorage,但在显示时往往是错误的.我发现ExifInterface可以确定图像的方向,并在必要时旋转和翻转它.

从手机上的图库区域选择图像时,出现此错误.

FileNotFoundError

我可以从图库中选择手机上的图像,它可以显示在页面上.

URI地址和数据之间的差异是一个 /

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

uri.toString() address: content:/media/external/images/media/53331
Run Code Online (Sandbox Code Playgroud)

我正在使用uri地址作为图像的绝对路径,以便能够在必要时旋转它.我将此值传递给另一个调用的方法modifyOrientation然后旋转它.一旦它被传递到方法,它就到达了这条线

ExifInterface ei = new ExifInterface(image_absolute_path);

然后在找不到文件时返回catch.

下面是我得到的整个错误以及我的所有代码.我该如何解决我遇到的这个问题.因此,当我将URI传递到下一个方法时,它实际上具有正确的地址.

整个错误

@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);
    }
Run Code Online (Sandbox Code Playgroud)

Com*_*are 7

A Uri不是File.

第一步:删除 File file = new File(uri.toString());

步骤2:确保您使用的是支持库版本ExifInterface

第3步:致电getContentResolver()Activity获得ContentResolver

第4步:调用openInputStream()ContentResolver,传递Uri,获得一个InputStream上的内容指出,由Uri

第五步:传递InputStreamExifInterface构造函数

步骤#6:使用ExifInterface当前的,确定图像方向

步骤#7:一旦工作正常,将所有这些I/O移动到后台线程

  • 我的错误是我使用将 `Uri` 传递给 ExifInterface 构造函数。从“Uri”创建一个输入流是解决方案。`contentResolver.openInputStream(uri)` (2认同)