首页 > 解决方案 > 图像在 bitmap.compress 后旋转

问题描述

所以我试图让用户从他们的照片库中选择一张图片,然后将其上传到 Firebase 存储......到目前为止,我的代码如下所示:

@Override
protected void onActivityResult(int requestCode,int resultcode, Intent data){
    super.onActivityResult(requestCode,resultcode,data);

    if(requestCode == REQUEST_IMAGE_CAPTURE && resultcode == RESULT_OK) {
        final Uri uri = data.getData();

        if (uri != null){
            Bitmap bmp = null;
            try {

                bmp = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);

            } catch (IOException e){
                e.printStackTrace();
            }
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.PNG, 25, baos);



            byte[] fileInBytes = baos.toByteArray();
            bmp.recycle();
            FirebaseAuth auth = FirebaseAuth.getInstance();
            FirebaseStorage storage = FirebaseStorage.getInstance();
            String imageString = "";
            if(clickPhoto ==1){imageString = "Image 1"; }
            else if(clickPhoto ==2){imageString = "Image 2"; }
            else if (clickPhoto ==3){imageString = "Image 3"; }
            else if (clickPhoto ==4){imageString = "Image 4"; }
            final StorageReference storageReference = storage.getReference().child("Images").child("users").child(auth.getCurrentUser().getUid()).child(imageString);
            storageReference.putBytes(fileInBytes).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                        @Override
                        public void onSuccess(Uri uri) {
                            String downloadUrl = uri.toString();
                            String urlString ="";
                            if(clickPhoto ==1){ urlString = "photo1URI";}
                            if(clickPhoto ==2){ urlString = "photo2URI";}
                            else if (clickPhoto ==3){ urlString = "photo3URI";}
                            else if (clickPhoto ==4){urlString = "photo4URI";}

                            toMap.put(urlString, downloadUrl);
                            updatedb();
                            toMap.clear();
                            Log.d("uriii",downloadUrl);}


                    });
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.d("UploadFailure",e.getMessage());
                }
            });




        }
        else {
            Toast.makeText(this,"Didn't work",Toast.LENGTH_LONG).show();
        }
    }
}

我的问题是上传的图像改变了方向......似乎是随机的......我如何保持方向?我查看了 Exif 界面,但看不到将其作为 ByteArrayOutputStream 上传的答案?

任何帮助,将不胜感激。

标签: javaandroidexif

解决方案


问题是图像中的 EXIF 信息(包括方向)在压缩后被删除。您需要将 EXIF 信息从原始图像复制到压缩图像。解决方案解释here


推荐阅读