は、私はこのように私のアプリでは、ユーザーの画像として使用する画像を選択していて回転:onActivityResult()で[メディアの選択やクラウドからの画像とは、正しく
Intent pickPhoto = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto, IMAGE_GALLERY);
if(requestCode == IMAGE_GALLERY && resultCode == RESULT_OK) {
Uri uri = intent.getData();
if(uri != null) {
this.picture = Utils.ScaleBitmap(context, uri, 640);
userPic.setScaleType(ImageView.ScaleType.CENTER_CROP);
userPic.setPadding(0,0,0,0);
userPic.setImageBitmap(picture);
}
}
私Utils.ScaleBitmap方法は以下の通りです:
try {
//Getting file path from URI
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = context.getContentResolver().query(imageURI, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
//Getting EXIF info to rotate image
ExifInterface exif = null;
try {
File pictureFile = new File(picturePath);
exif = new ExifInterface(pictureFile.getAbsolutePath());
} 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:
bitmap = rotateBitmap(bitmap, 90);
break;
case ExifInterface.ORIENTATION_ROTATE_180:
bitmap = rotateBitmap(bitmap, 180);
break;
case ExifInterface.ORIENTATION_ROTATE_270:
bitmap = rotateBitmap(bitmap, 270);
break;
}
//Compressing image
int w = bitmap.getWidth(), h = bitmap.getHeight();
int width, height;
if (w > max || h > max) {
if (w == h) {
width = height = max;
} else if (w < h) {
height = max;
width = max * w/h;
} else {
width = max;
height = max * h/w;
}
} else {
width = w;
height = h;
}
//Bitmap bitmap = ((BitmapDrawable) commentImage.getDrawable()).getBitmap();
Bitmap scaledphoto = Bitmap.createScaledBitmap(bitmap, width, height, true);
return scaledphoto;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
問題は、このコードは、私はFを選ぶイメージで動作しない、ということですGoogleドライブ、Picasaなどのクラウドを利用してください。
私はすべての回転するものをやっていないときに働いていました。それだけでした
Bitmap bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), imageURI);
そして私はどんなイメージを選んでも働くことができました。しかし、私のカメラからの画像は間違った向きになっていました。今はローテーションを修正しましたが、クラウドから画像を取得することはできません。
どのように私は両方のことを動作させることができます知っていますか?
つまり、クラウドストレージから画像を選択できるようになり、間違った向きの画像を回転できるようになります。
に解決策を見つけることでした "このコードは私が雲から選んだ画像では機能しませんe Googleドライブ、Picasaなど) - このコードには、クラウドに関連するものは何もないようです。あなたはローカルである 'MediaStore'から画像を選んでいます。どのようにクラウドから画像を選んでいますか? – CommonsWare
ギャラリーを開くと、Picasaフォルダが表示されます。 Photosアプリでインテントを実行すると私はphotos.google.comの写真を選択できます。したがって、ACTION_PICKはローカルピクチャだけではありません。 –
'File pictureFile = new File(picturePath)' - これは 'file'スキームを持つ' Uri'に対してのみ機能します。おそらく、あなたは 'content'のような異なるスキームを得ているでしょう。 PicassoやGlideなどのライブラリを使用する開発者は、画像の回転、スケーリングなどをライブラリの一部として取得します。このようなライブラリを使用したくない場合は、[ファイルではなくストリームで動作する 'ExifInterface'を使用してください(https://commonsware.com/blog/2016/05/31/tale-two-exifinterfaces.html )。 – CommonsWare