前提・実現したいこと
ギャラリーから正しい向きで写真を取り出したいです.
発生している問題・エラーメッセージ
現在のコードでは,写真を撮ってそのままimageviewに張り付ける場合には上手くいくのですが,
ギャラリーから選択する場合はなぜか上手くいく時といかない時があります.
該当のソースコード
@Override protected void onActivityResult(int requestCode, int resultCode, Intent intent) { super.onActivityResult(requestCode, resultCode, intent); if(requestCode == RESULT_CAMERA) { if(resultCode != RESULT_OK) { // キャンセル時 return ; } Uri resultUri = (intent != null ? intent.getData() : m_uri); if(resultUri == null) { // 取得失敗 return; } // ギャラリーへスキャンを促す MediaScannerConnection.scanFile( this, new String[]{resultUri.getPath()}, new String[]{"image/jpeg"}, null ); // 画像を設定 try { bitmapp = MediaStore.Images.Media.getBitmap(this.getContentResolver(),resultUri); } catch (IOException e) { e.printStackTrace(); } // 画像の横、縦サイズを取得 int imageWidth = bitmapp.getWidth(); int imageHeight = bitmapp.getHeight(); int correctDegree1; if(getFilePath(getApplication() , resultUri) != null) { //カメラから correctDegree1 = getRotateDegree(getFilePath(getApplication(), resultUri)); }else{ //ギャラリーから assert intent != null; correctDegree1 = getRotateDegree(getPath2(intent)); } Matrix matrix = new Matrix(); matrix.setRotate(correctDegree1, (float) imageWidth/2, (float) imageHeight/2); Bitmap bitmap3 = Bitmap.createBitmap(bitmapp, 0, 0, imageWidth, imageHeight, matrix, true); imageView.setImageBitmap(bitmap3); } private void showGallery() { //カメラの起動Intentの用意 String photoName = System.currentTimeMillis() + ".jpg"; ContentValues contentValues = new ContentValues(); contentValues.put(MediaStore.Images.Media.TITLE, photoName); contentValues.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); m_uri = getContentResolver() .insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues); Intent intentCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, m_uri); // ギャラリー用のIntent作成 Intent intentGallery; intentGallery = new Intent(Intent.ACTION_OPEN_DOCUMENT); intentGallery.addCategory(Intent.CATEGORY_OPENABLE); intentGallery.setType("image/jpeg"); Intent intent = Intent.createChooser(intentCamera, "画像の選択"); intent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {intentGallery}); startActivityForResult(intent, RESULT_CAMERA); } //アプリから起動したカメラで撮影した写真のファイルパスを取得 public static String getFilePath(Context context, Uri uri) { ContentResolver contentResolver = context.getContentResolver(); String[] columns = { MediaStore.Images.Media.DATA }; Cursor cursor = contentResolver.query(uri, columns, null, null, null); assert cursor != null; cursor.moveToFirst(); String path = cursor.getString(0); cursor.close(); return path; } //角度を取得 private int getRotateDegree(String filePath) { int degree; try { ExifInterface exifInterface = new ExifInterface(filePath); int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED); if (orientation == ExifInterface.ORIENTATION_ROTATE_90) { degree = 90; } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) { degree = 180; } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) { degree = 270; }else{ degree = 0; } if (degree != 0) { exifInterface.setAttribute(ExifInterface.TAG_ORIENTATION, "0"); exifInterface.saveAttributes(); } } catch (IOException e) { degree = -1; e.printStackTrace(); } return degree; } //ギャラリーからファイルパスを取得 private String getPath2(Intent intent){ String strDocId = DocumentsContract.getDocumentId(intent.getData()); String[] strSplittedDocId = strDocId.split(":"); String strId = strSplittedDocId[strSplittedDocId.length - 1]; Cursor crsCursor = getContentResolver().query( MediaStore.Images.Media.EXTERNAL_CONTENT_URI , new String[] {MediaStore.MediaColumns.DATA} , "_id=?", new String []{strId}, null ); assert crsCursor != null; crsCursor.moveToFirst(); String path2 = crsCursor.getString(0); crsCursor.close(); return path2; }
ギャラリーから正しい向きで画像を取得,表示できない原因と解決策が分かる方がいらっしゃれば,ご教授ください.
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/08/14 07:37
2020/08/14 07:46 編集