2017-05-29 309 views
0

我想檢查位圖的方向並在需要時將其翻轉,但我在應用代碼時出錯。這裏是我的代碼,而我試圖用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) 

回答

5

您嘗試使用android.media.ExifInterface。在Android 7.0+(API Level 24)上,該類可以安全使用,並且構造函數採用InputStream。顯然,您正在舊設備上運行您的應用程序。這導致了兩個問題:

  1. 老設備不會再有這樣的構造

  2. ExifInterface對舊設備的安全漏洞,打開您的應用最多的惡意軟件攻擊

使用android.support.media.ExifInterface代替。它來自支持庫(具體地說,是com.android.support:exifinterface)。它提供了一個構造函數,它支持在Android的所有支持版本上運行的InputStream。並且,它繞過了舊設備上的安全漏洞。

+0

謝謝,它不再給出錯誤。但它總是在所有圖像上返回0。 –

+0

@SalehRefaai:這並不奇怪,因爲'Bitmap'沒有任何EXIF標頭。當您從任何地方加載「位圖」時,您已經丟棄了任何EXIF數據。在原始數據源上使用'ExifInterface',而不是'Bitmap'。 – CommonsWare

2

添加到您的build.gradle文件

compile "com.android.support:exifinterface:25.1.0" 

對我來說它的工作。