java - 使用 ExifInterface 时出现 FileNotFoundException

标签 java android rotation exif filenotfoundexception

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

在我的手机上从图库区域选择图像时出现此错误。

FileNotFoundError

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

URI地址和数据的区别是一个/

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

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

我使用 uri 地址作为图像的图像绝对路径,以便能够在必要时旋转它。我将这个值传递给另一个名为 modifyOrientation 的方法,然后旋转它。一旦它被传递到方法中,它就会到达行

ExifInterface ei = new ExifInterface(image_absolute_path);

然后由于找不到文件而返回到 catch。

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

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);
    }

最佳答案

Uri 不是文件

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

第 2 步:确保您使用的是 the Support Library edition of ExifInterface

第 3 步:在您的 Activity 上调用 getContentResolver() 以获取 ContentResolver

第 4 步:在 ContentResolver 上调用 openInputStream(),传入 Uri,以获取 InputStreamUri

指向的内容上

第 5 步:将 InputStream 传递给 the ExifInterface constructor

第 6 步:像现在一样使用 ExifInterface 来确定图像方向

第 7 步:一旦一切正常,将所有这些 I/O 移至后台线程

关于java - 使用 ExifInterface 时出现 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49407931/

相关文章:

java - SOA Suite 到 Axis2 数据被丢弃

android - 旋转图片错误: java. lang.OutOfMemoryError

ios - viewWillTransitionToSize : vs willTransitionToTraitCollection:

java - 在 Java 应用程序中实现插件?

Java - 代码编译但无法识别操作

java - 在 java 类和 android Activity 之间流式传输时音频不清晰

c# - 将 FusedLocationApi 与 Xamarin 3 结合使用

android - 如果通过 CMD 打开,模拟器只有互联网连接

android - Android 手机一次在后台运行多少个最大应用程序

iOS5 打破了 UISplitViewController 旋转回调,如何手动调用?