使用ExifInterface时的FileNotFoundException [英] FileNotFoundException when Using ExifInterface
问题描述
我一直在将图片上传到FirebaseStorage,但在展示时往往是错误的。我发现ExifInterface可以确定图像的方向,并在必要时旋转和翻转它。
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.
URI地址和数据之间的差异是一个 /
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
我正在使用uri地址作为图像如果需要,图像的绝对路径可以旋转它。我将此值传递给另一个名为 modifyOrientation
的方法,然后将其旋转。一旦传递到方法,它就到达了行
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);
然后在找不到文件时返回到catch。
and then returns to the catch as the file was not found.
以下是我得到的整个错误以及我的所有代码。我该如何解决我遇到的这个问题。因此,当我将URI传递到下一个方法时,它实际上具有正确的地址。
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);
}
推荐答案
A Uri
不是文件
。
步骤1:删除文件文件=新文件(uri.toString());
步骤2:确保您使用 支持库版本ExifInterface
Step #2: Make sure that you are using the Support Library edition of ExifInterface
步骤3:在活动上调用
获取 getContentResolver()
ContentResolver
步骤4:致电
,传入 ContentResolver
上的openInputStream() Uri
,获得 InputStream
关于 Uri所指向的内容
Step #4: Call openInputStream()
on the ContentResolver
, passing in the Uri
, to get an InputStream
on the content pointed to by that Uri
步骤# 5:将 InputStream
传递给 ExifInterface
构造函数
Step #5: Pass that InputStream
to the ExifInterface
constructor
步骤#6:使用 ExifInterface
就像你现在一样,确定图像方向
Step #6: Use that ExifInterface
as you are presently, to determine the image orientation
步骤7:一旦你开始工作,移动所有这些我/ O到后台线程
Step #7: Once you get things working, move all of this I/O to a background thread
这篇关于使用ExifInterface时的FileNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!