2016-03-29 18 views
0

次のコードを使用して選択した画像の向きを取得していますので、ギャラリーから選択すると縦に撮った場合は水平に表示されません。ギャラリーから選択したときに画像が回転しました

ファイルでエラーが発生しています。imageFile = new File(selectedImage.toString());パラメータselectedImage.toString()内で、最初のint rotate = 90を変更して垂直イメージを選択したので、結果は良好でした。

ファイルにパラメータを正しく渡していますか?

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    BitmapFactory.Options options; 
    if (resultCode == RESULT_OK && requestCode == PICTURE_SELECTED) { 
     try { 
      options = new BitmapFactory.Options(); 
      options.inSampleSize = 4; 
      Uri selectedImage = data.getData(); 
      InputStream stream = getContentResolver().openInputStream(selectedImage); 
      Bitmap yourSelectedImage = BitmapFactory.decodeStream(stream, null, options); 
      stream.close(); 
      //orientation 
      try { 
       int rotate = 0; 
       try { 
        File imageFile = new File(selectedImage.toString()); 
        ExifInterface exif = new ExifInterface(
          imageFile.getAbsolutePath()); 
        int orientation = exif.getAttributeInt(
          ExifInterface.TAG_ORIENTATION, 
          ExifInterface.ORIENTATION_NORMAL); 

        switch (orientation) { 
         case ExifInterface.ORIENTATION_ROTATE_270: 
          rotate = 270; 
          break; 
         case ExifInterface.ORIENTATION_ROTATE_180: 
          rotate = 180; 
          break; 
         case ExifInterface.ORIENTATION_ROTATE_90: 
          rotate = 90; 
          break; 
        } 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       Matrix matrix = new Matrix(); 
       matrix.postRotate(rotate); 
       yourSelectedImage = Bitmap.createBitmap(yourSelectedImage , 0, 0, yourSelectedImage.getWidth(), yourSelectedImage.getHeight(), matrix, true); } 
      catch (Exception e) {} 
      //end of orientation 

      imgButton.setScaleType(ImageView.ScaleType.FIT_CENTER); 
      imgButton.setImageBitmap(yourSelectedImage); 
     } catch (Exception e) { 
      Toast.makeText(getApplicationContext(), "Could not open file.", Toast.LENGTH_LONG).show(); 
     } 
    } else { 
     Toast.makeText(getApplicationContext(), "Image was not selected", Toast.LENGTH_LONG).show(); 
    } 
    super.onActivityResult(requestCode, resultCode, data); 
} 
+0

画像は、この<活動 アンドロイドのようにギャラリーを開設しているあなたの活動中にあなたマニフェストにこの行を追加します。name =「ui.ui.activity.MainActivity。」 アンドロイド:configChangesは= "keyboardHidden |オリエンテーション| screenSize " android:label =" @ string/title_activity_main " android:screenOrientation =" portrait "/> –

+0

このコードは少し役立ちましたか? –

+0

いいえ、エラー:android:configChangesとscreenOrientationは許可されていません – cgeekmt

答えて

1

はこのようにあなたの方法を変更します。

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    BitmapFactory.Options options; 
    if (resultCode == RESULT_OK && requestCode == PICTURE_SELECTED) { 
     try { 
      options = new BitmapFactory.Options(); 
      options.inSampleSize = 4; 
      Uri selectedImage = data.getData(); 

      String[] filePath = {MediaStore.Images.Media.DATA}; 

      Cursor cursor = getContentResolver().query(selectedImage, filePath, null, null, null); 
        cursor.moveToFirst(); 
      String mImagePath = cursor.getString(cursor.getColumnIndex(filePath[0])); 

      InputStream stream = getContentResolver().openInputStream(selectedImage); 
      Bitmap yourSelectedImage = BitmapFactory.decodeStream(stream, null, options); 
      stream.close(); 
      //orientation 
      try { 
       int rotate = 0; 
       try { 
        ExifInterface exif = new ExifInterface(
          mImagePath); 
        int orientation = exif.getAttributeInt(
          ExifInterface.TAG_ORIENTATION, 
          ExifInterface.ORIENTATION_NORMAL); 

        switch (orientation) { 
         case ExifInterface.ORIENTATION_ROTATE_270: 
          rotate = 270; 
          break; 
         case ExifInterface.ORIENTATION_ROTATE_180: 
          rotate = 180; 
          break; 
         case ExifInterface.ORIENTATION_ROTATE_90: 
          rotate = 90; 
          break; 
        } 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
       Matrix matrix = new Matrix(); 
       matrix.postRotate(rotate); 
       yourSelectedImage = Bitmap.createBitmap(yourSelectedImage , 0, 0, yourSelectedImage.getWidth(), yourSelectedImage.getHeight(), matrix, true); } 
      catch (Exception e) {} 
      //end of orientation 

      imgButton.setScaleType(ImageView.ScaleType.FIT_CENTER); 
      imgButton.setImageBitmap(yourSelectedImage); 
     } catch (Exception e) { 
      Toast.makeText(getApplicationContext(), "Could not open file.", Toast.LENGTH_LONG).show(); 
     } 
    } else { 
     Toast.makeText(getApplicationContext(), "Image was not selected", Toast.LENGTH_LONG).show(); 
    } 
} 
+0

ありがとうございました:) – cgeekmt

+0

私はうれしいです!私の答えを投票してください。ありがとう! –

+0

私はそれを正しい答えとしてマークしました。私は最初に15の評判が必要なので投票できません。ありがとう – cgeekmt

0

画像処理のためのピカソが最高のライブラリはあなたもキャッシュから画像をロードし、アプリのパフォーマンスを向上させることができ、あなたが簡単にそれを使用して画像を処理することができ、それを使用しているコンパイルだからたくさんのコードをやることを避け、ビルドgradleに次の行を追加して、あなたのコードでpicassoを使用してみてください。

compile 'com.squareup.picasso:picasso:2.5.0' 
+0

ピカソを使用して問題を解決していない、画像はまだ回転している – Curio

関連する問題