2015-11-04 46 views
0

我試圖將圖片從URI複製到文件路徑。然後我從路徑中讀取圖片,但是我得到的圖片旋轉了90度。以下是我的功能。任何人都可以幫忙嗎?將圖片從URI複製到文件時旋轉了90度的圖片

public boolean copyPicture(Context context, Uri source, String dest) { 
    boolean result = false; 
    int bytesum = 0; 
    int byteread = 0; 
    File destFile = new File(dest); 
    String scheme = source.getScheme(); 
    if (ContentResolver.SCHEME_CONTENT.equals(scheme) 
      || ContentResolver.SCHEME_FILE.equals(scheme)) { 
     InputStream inStream = null; 
     try { 
      inStream = context.getContentResolver().openInputStream(source); 
      if (!destFile.exists()) { 
       result = destFile.createNewFile(); 
      } 
      if (result) { 
       FileOutputStream fs = new FileOutputStream(dest); 
       byte[] buffer = new byte[1024]; 
       while ((byteread = inStream.read(buffer)) != -1) { 
        bytesum += byteread; //字節數 文件大小 
        System.out.println(bytesum); 
        fs.write(buffer, 0, byteread); 
       } 
       inStream.close(); 
       fs.flush(); 
       fs.close(); 
      } 
     } catch (Exception e) { 
      e.printStackTrace(); 
      result = false; 
     } 
    } 
    return result; 
} 
+0

http://stackoverflow.com/questions/33406377/how-to-solve-image-capturing-automatically-rotating-90-degrees-in-android/33406434#33406434 –

+0

感謝Anoop。但我想知道爲什麼圖片在複製功能期間旋轉。或者我怎樣才能避免旋轉。 –

回答

1

EXIF接口就是答案。它允許您從圖像文件中讀取指定的屬性。

Bitmap bitmap = BitmapFactory.decodeFile(path, options); 
       try { 

         ExifInterface exif = new ExifInterface(path); 
         int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 

         int angle = 0; 

         if (orientation == ExifInterface.ORIENTATION_ROTATE_90) { 
          angle = 90; 
         } 
         else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) { 
          angle = 180; 
         } 
         else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) { 
          angle = 270; 
         } 
         Matrix mat = new Matrix(); 
         mat.postRotate(angle); 
         Bitmap correctBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), mat, true);     
        bitmap=correctBmp; 
       } 
       catch(Exception e){ 

       }