图像在bitmap.compress之后旋转 [英] Image rotates after bitmap.compress

查看:111
本文介绍了图像在bitmap.compress之后旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So i'm trying to get the user to pick an image from their photo gallery then upload it to Firebase Storage...so far my code looks as follows:

@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上传的答案吗?

My problem is that the image that is uploaded changes orientation...seeminly at random....how do i maintain orientation? I've looked at Exif interface but can't see an answer for uploading it as a ByteArrayOutputStream?

任何帮助将不胜感激.

推荐答案

问题是压缩后会删除图像中的EXIF信息(包括方向).您需要将EXIF信息从原始图像复制到压缩图像中.解决方案此处.

Issue is that EXIF information from image (including orientation) is removed after compressing. You need to copy EXIF info from original image to compressed one. Solution explained here.

这篇关于图像在bitmap.compress之后旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆