使用ExifInterface时的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传递给下一个方法时,它实际上具有正确的地址。

整个错误

 @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不是File

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

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

步骤3:在Activity上调用getContentResolver()以获取ContentResolver

步骤#4:在ContentResolver上调用openInputStream() ,传入Uri ,以获取该Uri指向的内容的InputStream

步骤#5:将InputStream传递给ExifInterface构造函数

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

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

我尝试了CommonsWare的解决方案,它确实有效,但显然带有InputStream参数的ExifInterface构造函数仅在API 24中添加。有没有办法仍然使用String构造函数?

猜测答案可能在第2步 – 但我如何确保我使用的是那个版本的ExifInterface?