上传到Firebase存储后,图像自动旋转

时间:2019-07-27 15:24:48

标签: android firebase-storage

我有一个应用程序,可以将用户图像上传到Firebase存储中。 从本地发出Intent后,图像可以正确显示在imageView中,但是在上传到Firebase之后,它会旋转为横向。

这是我尝试的代码。

private void updatePhoto() {
        if(resultUri != null) {

            final StorageReference filePath = FirebaseStorage.getInstance().getReference().child("profile_images").child(userID);
            Bitmap bitmap = null;
            try {
                bitmap = MediaStore.Images.Media.getBitmap(getApplication().getContentResolver(), resultUri);
            } catch (Exception e) {
                e.printStackTrace();
            }

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 20, baos);
            byte[] data = baos.toByteArray();
            final UploadTask uploadTask = filePath.putBytes(data);

            uploadTask.addOnFailureListener(e -> {
                finish();
                return;
            });

            final ProgressDialog progressDialog = new ProgressDialog(this);
            progressDialog.setTitle("Uploading...");
            progressDialog.show();

            uploadTask.addOnSuccessListener(taskSnapshot -> {

                filePath.getDownloadUrl().addOnSuccessListener(uri -> {
                    Map newImage = new HashMap();
                    newImage.put("profileImage", uri.toString());
                    userDatabase.updateChildren(newImage);
                    progressDialog.dismiss();
                    finish();
                });

                return;
            });
        }else{
            finish();
        }
    }

这是我的onActivityResult

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 1 && resultCode == Activity.RESULT_OK){
            final Uri imageUri = data.getData();
            resultUri = imageUri;
            mProfileImage.setImageURI(resultUri);
        }
    }

1 个答案:

答案 0 :(得分:0)

这里的代码可在上传到Firebase之前更正图像位置

  

检查图像是否需要旋转

    private  fun rotateImageIfRequired( context:Context, img:Bitmap,  selectedImage:Uri):Bitmap {

    // Detect rotation
    var rotation = getRotation( context, selectedImage)
    if (rotation != 0) {
         var matrix:Matrix =  Matrix()
        matrix.postRotate(rotation as Float)
        var  rotatedImg = Bitmap.createBitmap(img, 0, 0, img.getWidth(), img.getHeight(), matrix, true)
        img.recycle()
        return rotatedImg
    }
    else{
        return img
    }
}
  

旋转图片

fun getRotation( context:Context, imageSelected: Uri):Int{
        var rotation = 0
        var content: ContentResolver = context.contentResolver
        var arr:Array<String> = arrayOf("orientation","date_added")

        val mediaCursor:Cursor = content.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,  arr,
                                       null, null, "date_added desc")
        if (mediaCursor != null && mediaCursor.getCount() != 0) {
            while(mediaCursor.moveToNext()){
                rotation = mediaCursor.getInt(0)
                break
            }
        }
        mediaCursor.close()
        return rotation

    }