2017-10-19 1018 views
0

我在Android应用程序中使用了ExifInterface来获取图像的相机方向。我的代码完美适用于。但是当构建版本大于或等于24时,它会在android studio logcat中发出警告,并且无法正常工作。ExifInterface得到了不支持的图像

下面是onActivityResult方法,我的代码块:

if(requestCode == REQUEST_CAPTURE_IMG && resultCode == RESULT_OK) { 
      Log.d(TAG, "Inside camera operation"); 
      int reqWidth = 480, reqHeight = 800; 
      try { 
       InputStream inStream = null; 
       try { 
        inStream = getContentResolver().openInputStream(imageUri); 

        //Decode image size 
        BitmapFactory.Options options = new BitmapFactory.Options(); 
        options.inJustDecodeBounds = true; 

        BitmapFactory.decodeStream(inStream, null, options); 
        inStream.close(); 

        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); 

        options.inJustDecodeBounds = false; 
        inStream = getContentResolver().openInputStream(imageUri); 

        CommonStaticClass.mImage = BitmapFactory.decodeStream(inStream, null, options); 

        ExifInterface exif = null; 

        try { 
         //File pictureFile = new File(imgDecodableString); 
         if (Build.VERSION.SDK_INT >= 24) { 
          exif = new ExifInterface(inStream); 
          Log.d("exif", "sdk 24"); 
         } 
         else { 
          exif = new ExifInterface(imageUri.getPath()); 
         } 
        } catch (IOException e) { 
         e.printStackTrace(); 
        } 

        int orientation = ExifInterface.ORIENTATION_NORMAL; 

        if (exif != null) 
         orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 

        switch (orientation) { 
         case ExifInterface.ORIENTATION_ROTATE_90: 
          CommonStaticClass.mImage = rotateBitmap(CommonStaticClass.mImage, 90); 
          break; 
         case ExifInterface.ORIENTATION_ROTATE_180: 
          CommonStaticClass.mImage = rotateBitmap(CommonStaticClass.mImage, 180); 
          break; 

         case ExifInterface.ORIENTATION_ROTATE_270: 
          CommonStaticClass.mImage = rotateBitmap(CommonStaticClass.mImage, 270); 
          break; 
        } 

        inStream.close(); 
       } catch (IOException e) { 
        //Toast.makeText(this, "IO exception", Toast.LENGTH_SHORT).show(); 
        Toast.makeText(this, SelectSuitActivity.this.getString(R.string.wrong_msg), Toast.LENGTH_SHORT).show(); 
       } 
       //CommonStaticClass.mImage = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri); 
       startActivity(intent); 
      } catch (Exception e) { 
       Toast.makeText(this, SelectSuitActivity.this.getString(R.string.wrong_msg), Toast.LENGTH_SHORT).show(); 
      } 
     } 

这里是logcat的结果,当我拿起从相机图像:

10-19 12:39:04.399 2912-2912/com.example.myapp I/ExifInterface_JNI: Corrupted image. 
    10-19 12:39:04.414 2912-2912/com.example.myapp W/ExifInterface: Invalid image: ExifInterface got an unsupported image format file(ExifInterface supports JPEG and some RAW image formats only) or a corrupted JPEG file to ExifInterface. 
    java.io.EOFException 
    at java.io.DataInputStream.readByte(DataInputStream.java:270) 
    at android.media.ExifInterface.getJpegAttributes(ExifInterface.java:1834) 
    at android.media.ExifInterface.loadAttributes(ExifInterface.java:1475) 
    at android.media.ExifInterface.<init>(ExifInterface.java:1174) 
    at com.lostsym.founder.SelectSuitActivity.onActivityResult(SelectSuitActivity.java:621) 
    at android.app.Activity.dispatchActivityResult(Activity.java:6932) 
    at android.app.ActivityThread.deliverResults(ActivityThread.java:4085) 
    at android.app.ActivityThread.handleSendResult(ActivityThread.java:4132) 
    at android.app.ActivityThread.-wrap20(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:154) 
    at android.app.ActivityThread.main(ActivityThread.java:6119) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

这将是有益的,如果任何人都可以找出什么是这里错了。谢谢。

+0

您的jpg文件已损坏。或者您使用不支持的图像格式。 – greenapps

+2

您在BitmapFactory使用过之后使用inStream。这是不可能的。另一次打开流。 – greenapps

+0

@greenapps非常感谢。你的建议解决了我的问题。 –

回答

1

您在BitmapFactory使用它之后使用inStream。这是不可能的。另一次打开流。