【发布时间】:2015-06-25 14:11:32
【问题描述】:
我正在尝试使用 Android 上的意图选择一张照片。一切正常,并且可以从任何照片应用程序(相机、画廊、屏幕截图等)中检索照片,即使是从新的照片应用程序中选择的;除了在 Google 照片上在线备份的那些。
获取位图时,会以横向方式检索纵向拍摄的照片。我有获取照片方向的代码,因此我可以进行相应的翻译,但在新的 Google 照片应用程序上选择在线照片时,方向始终为 0。关于如何在返回 0 时获得这些照片的方向的任何想法?下面的代码 - 谢谢。
初衷
Intent i = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, IMPORT_PHOTO_RESULT);
意图结果
Uri selectedImageUri = data.getData();
imageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);
//fix rotation
int orientation = -1;
Cursor cursor = MediaStore.Images.Media.query(getContentResolver(), selectedImageUri, new String[] { MediaStore.Images.ImageColumns.ORIENTATION });
try {
if (cursor.moveToFirst()) {
orientation = cursor.getInt(0);
}
} finally {
cursor.close();
}
imageBitmap = Utils.rotateImageWithRotation(orientation, imageBitmap);
//get stream to display and send
ByteArrayOutputStream stream = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream);
// get byte array here
byte[] bytearray = stream.toByteArray();
sendImage(bytearray, null);
旋转图像
public static Bitmap rotateImageWithRotation(int rotationInDegrees, Bitmap mBitmap) {
Matrix matrix = new Matrix();
if (rotationInDegrees != 0f) {
matrix.preRotate(rotationInDegrees);
}
Bitmap adjustedBitmap = Bitmap.createBitmap(mBitmap, 0, 0, mBitmap.getWidth(), mBitmap.getHeight(), matrix, true);
return adjustedBitmap;
}
【问题讨论】:
标签: android android-intent android-camera orientation