2017-04-26 163 views
1

我有這個代碼在用戶選擇後在圖像按鈕顯示圖像。android - 圖片旋轉在圖像按鈕

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data){ 
    super.onActivityResult(requestCode, resultCode, data); 

    Uri selectedImageUri = data.getData(); 
    if (null != selectedImageUri) { 
     // Set the image in ImageView 
     _changePicture.setImageURI(selectedImageUri); 
    } 
} 

我的問題是,當選擇一個大圖像時,它會顯示左旋90度。我在這裏找到了一些解決方案在stackoverflow.com,但我不能使用它們中的任何一個,因爲它們使用「圖像」,而我只使用「Uri」。

編輯: 使用代碼像這樣的:

public static void rotateImageIfRequired(Uri selectedImage) throws IOException { 

     ExifInterface ei = new ExifInterface(selectedImage.getPath()); 
     int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 

     switch (orientation) { 
      case ExifInterface.ORIENTATION_ROTATE_90: 
       Log.e(TAG,"ROTATE = 90"); 
      case ExifInterface.ORIENTATION_ROTATE_180: 
       Log.e(TAG,"ROTATE = 180"); 
       //return rotateImage(img, 180); 
      case ExifInterface.ORIENTATION_ROTATE_270: 
       Log.e(TAG,"ROTATE = 270"); 
       //return rotateImage(img, 270); 
      default: 
       Log.e(TAG,"ROTATE = 0"); 
       //return img; 
     } 
    } 

「ROTATE = 0」 被返回。一些圖像是幾天前拍攝的。他們都很大。在小圖像中不發生旋轉。

+2

其分毫用imagebutton做。我假設你正在從相機中獲取圖片?您需要以您想要的方向請求它,或者自己手動旋轉並重新保存它 – Blundell

+0

我知道這一點。我不知道怎麼做。 –

回答

1

你嘗試過使用畢加索圖書館嗎? (http://square.github.io/picasso/)。如果您想了解更多信息,可以在GitHub上關注他們的示例。

對於從設備可以使用加載圖像:文件F =新的文件( 「路徑到圖像/ image.png」)

或 文件f =新的文件(URI)

。Picasso.with(getActivity())負荷(F).into(ImageView的);

或 Picasso.with(getActivity()).load(uri).into(imageView);

畢加索還提供IMAGE TRANSMORMATIONS:

Picasso.with(上下文) .load(URL) .resize(50,50) .centerCrop() .into(ImageView的)

+0

非常感謝!這對我來說工作得很好! –

+0

很高興能幫到你!快樂編碼! :) –