这个问题可能是由于图片的方向信息不正确导致的。为了解决这个问题,你可以尝试使用 ExifInterface 类来从图片中获取方向信息,并旋转它以使其正常显示。在上传图片之前,可以尝试对图片进行修复,代码示例如下:
// Get the BitmapFactory options object.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
// Decode the file to get the info about it.
BitmapFactory.decodeFile(filePath, options);
// Get the orientation of the image from the Exif data.
ExifInterface exif = new ExifInterface(filePath);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
// Calculate the sample size to use for decoding the image.
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode the file again, setting the sample size.
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(filePath, options);
// Rotate the bitmap according to the orientation.
bitmap = rotateBitmap(bitmap, orientation);
// Upload the rotated bitmap to Firebase.
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
byte[] data = outputStream.toByteArray();
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReference();
StorageReference imageRef = storageRef.child("images/" + filename);
UploadTask uploadTask = imageRef.putBytes(data);
其中 calculateInSampleSize()
和 rotateBitmap()
这两个方法的实现与正常使用时一样,这里不再赘述。