图像旋转后bitmap.compress

时间:2018-07-12 16:26:46

标签: java android exif

所以我试图让用户从他们的照片库中选择一张图像,然后将其上传到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上传的答案吗?

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

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