【发布时间】:2019-01-12 17:39:44
【问题描述】:
我正在尝试完成以下任务:
- 从图库中选择图像
- 使用 ExifInterface 将图像(如有必要)旋转到正确的方向
- 将图片上传到 Firebase
问题
如果图像需要旋转,我将得到一个旋转的位图文件。如何将此位图文件转换为 Uri,以便我可以上传到 Firebase?
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_INTENT && resultCode == Activity.RESULT_OK) {
mImageUri = data.getData();
try{
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getApplicationContext().getContentResolver(),mImageUri);
rotatedBitmap = rotateImageIfRequired(getContext(), bitmap, mImageUri);
mVideoImage.setImageBitmap(rotatedBitmap);
imageHeight = bitmap.getHeight();
imageWidth = bitmap.getWidth();
}catch (IOException e){
e.printStackTrace();
}
}
private static Bitmap rotateImageIfRequired(Context context, Bitmap img, Uri selectedImage) throws IOException {
InputStream input = context.getContentResolver().openInputStream(selectedImage);
ExifInterface ei;
if (Build.VERSION.SDK_INT > 23)
ei = new ExifInterface(input);
else
ei = new ExifInterface(selectedImage.getPath());
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
switch (orientation) {
case ExifInterface.ORIENTATION_ROTATE_90:
return rotateImage(img, 90);
case ExifInterface.ORIENTATION_ROTATE_180:
return rotateImage(img, 180);
case ExifInterface.ORIENTATION_ROTATE_270:
return rotateImage(img, 270);
default:
return img;
}
}
private static Bitmap rotateImage(Bitmap img, int degree) {
Matrix matrix = new Matrix();
matrix.postRotate(degree);
Bitmap rotatedImg = Bitmap.createBitmap(img, 0, 0, img.getWidth(), img.getHeight(), matrix, true);
img.recycle();
return rotatedImg;
}
【问题讨论】:
标签: android firebase image-rotation