4

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();
        }
    }
}

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?

Any help would be appreciated.

James Palfrey
  • 753
  • 6
  • 29
  • See this answer: https://stackoverflow.com/questions/20478765/how-to-get-the-correct-orientation-of-the-image-selected-from-the-default-image – mrmichaeldev Jul 12 '18 at 16:41
  • it doesn't solve my problem, as i still can't apply the exif interface to the bmp.compress(Bitmap.CompressFormat, quality, output) method? – James Palfrey Jul 12 '18 at 16:51
  • Check the answer here by Manuel for how to apply the rotation to the bitmap https://stackoverflow.com/questions/3647993/android-bitmaps-loaded-from-gallery-are-rotated-in-imageview – mrmichaeldev Jul 12 '18 at 17:05
  • It's not a case of applying it to the Bitmap, the Bitmap is compressed using bmp.compress and then output into a ByteArrayOutputStream before being uploaded into firebase....so i need to apply the rotation to the bytearrayoutput before it is uploaded? – James Palfrey Jul 12 '18 at 18:46

1 Answers1

6

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.

Valery Miller
  • 804
  • 1
  • 9
  • 13