【问题标题】:Uploading Rotated Image to Firebase将旋转图像上传到 Firebase
【发布时间】:2019-01-12 17:39:44
【问题描述】:

我正在尝试完成以下任务:

  1. 从图库中选择图像
  2. 使用 ExifInterface 将图像(如有必要)旋转到正确的方向
  3. 将图片上传到 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


    【解决方案1】:

    要将位图上传到 FireBase 存储,您需要将其转换为字节数组。您可以通过创建 ByteArrayOutputStream 来做到这一点。

    ByteArrayOutputStream boas = new ByteArrayOutputStream();
    

    然后将位图压缩成 JPEG 或 PNG 等格式。 compress 方法有 3 个参数,格式、质量和 ByteArrayOutputStream。

    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, boas);
    

    然后创建一个指向您的 FireBase 存储参考的参考,用于放置照片。

    String imageFileName = "ExampleName";    
    StorageReference ref = FirebaseStorage.getInstance().getReference().child("images").child(imageFileName);
    

    在这里,我创建了一个名为“images”的文件夹和一个使用先前创建的 imageFileName 字符串命名的文件

    然后我可以通过说

    使用 UploadTask 将其上传到 FireBase
    UploadTask task = ref.putBytes(data);
    

    使用此任务,您可以创建成功和失败侦听器。

     task.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
    
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
    
            }
        });
    

    【讨论】:

    • 感谢您的及时回复。我会试试你的方法。
    猜你喜欢
    • 2015-06-29
    • 2023-03-23
    • 2017-07-28
    • 2018-04-09
    • 2018-03-09
    • 2017-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多