使用ExifInterface时出现Android错误
问题描述:
我想检查位图的方向并在需要时将其翻转,但我在应用代码时出错。这里是我的代码,而我试图用ExifInterface到flipp图像:使用ExifInterface时出现Android错误
@RequiresApi(api = Build.VERSION_CODES.N)
public void flipping(Bitmap b)
{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.JPEG,100, bos);
byte[] bitmapdata = bos.toByteArray();
ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);
try {
ExifInterface exif = new ExifInterface(bs);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
ExifInterface.ORIENTATION_UNDEFINED);
switch(orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
rotateImage(b, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
rotateImage(b, 180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
rotateImage(b, 270);
break;
case ExifInterface.ORIENTATION_NORMAL:
default:
break;
}
encoding();
} catch (IOException e) {
e.printStackTrace();
}
}
public static Bitmap rotateImage(Bitmap source, float angle) {
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(),
matrix, true);
}
,这里是错误:
java.lang.NoSuchMethodError: No direct method <init>(Ljava/io/InputStream;)V in class Landroid/media/ExifInterface; or its super classes (declaration of 'android.media.ExifInterface' appears in /system/framework/framework.jar)
at com.sara.image_test.MainActivity.flipping(MainActivity.java:181)
at com.sara.image_test.MainActivity.onActivityResult(MainActivity.java:66)
at android.app.Activity.dispatchActivityResult(Activity.java:7165)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4994)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:5041)
at android.app.ActivityThread.access$1600(ActivityThread.java:229)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1875)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:7325)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
答
您尝试使用android.media.ExifInterface
。在Android 7.0+(API Level 24)上,该类可以安全使用,并且构造函数采用InputStream
。显然,您正在旧设备上运行您的应用程序。这导致了两个问题:
老设备不会再有这样的构造
ExifInterface
对旧设备的安全漏洞,打开您的应用最多的恶意软件攻击
使用android.support.media.ExifInterface
代替。它来自支持库(具体地说,是com.android.support:exifinterface
)。它提供了一个构造函数,它支持在Android的所有支持版本上运行的InputStream
。并且,它绕过了旧设备上的安全漏洞。
答
的ExifInterface构造你的代码示例中使用:
ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);
在SDK级别24中添加。
但它看起来像代码正在de副比24
较早版本的Android见https://developer.android.com/reference/android/media/ExifInterface.html#ExifInterface(java.io.InputStream)
答
添加到您的build.gradle文件
compile "com.android.support:exifinterface:25.1.0"
对我来说它的工作。
谢谢,它不再给出错误。但它总是在所有图像上返回0。 –
@SalehRefaai:这并不奇怪,因为'Bitmap'没有任何EXIF标头。当您从任何地方加载“位图”时,您已经丢弃了任何EXIF数据。在原始数据源上使用'ExifInterface',而不是'Bitmap'。 – CommonsWare