0

I am using Bitmap to reduce image size but unfortunately image rotate +90 degrees after uploading to Firebase Storage.

Original Image

Uploaded image

imageUploadActivity.java

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

    if (requestCode == 123 && resultCode== RESULT_OK && data!=null) {

     Uri imageUri = data.getData();
        Bitmap bmp = null;
        try {
            bmp = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
        } catch (IOException e) {
            e.printStackTrace();
        }
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.JPEG, 35, baos);
        byte[] datasa = baos.toByteArray();



        StorageReference filePath = UserProfilePics.child(UserID + ".jpg");

        filePath.putBytes(datasa).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
                if (task.isSuccessful()) {

                    Toast toast = new Toast(getApplicationContext());
                    toast.setDuration(Toast.LENGTH_LONG);

                    //inflate view
                    View custom_view = getLayoutInflater().inflate(R.layout.toast_icon_text, null);
                    ((TextView) custom_view.findViewById(R.id.message)).setText("Profile image updated successfully");
                    ((ImageView) custom_view.findViewById(R.id.icon)).setImageResource(R.drawable.ic_baseline_done_24);
                    ((CardView) custom_view.findViewById(R.id.parent_view)).setCardBackgroundColor(getResources().getColor(R.color.green_500));

                    toast.setView(custom_view);
                    toast.show();
    }
   }
 }
Alex
  • 53
  • 5
  • I think this will help you. https://stackoverflow.com/questions/47261434/photo-rotated-from-camera-samsung-device – Aniruddh Parihar Oct 03 '22 at 11:28
  • This solution does not work on Android 8+ – Alex Oct 03 '22 at 16:21
  • `am using Bitmap to reduce image size but unfortunately image rotate +90 degrees after uploading to Firebase Storage.` No. Has nothing to do with uploading. Your bitmap already contains a rotated image. Assign bitmap to an imageview and you will see. – blackapps Oct 03 '22 at 16:31
  • No, it's not, i tried with more than 10 images, when i used regular upload method "URI" The image remains the same as it was in the gallery. also i would like to thank you for your comment. – Alex Oct 03 '22 at 17:25

1 Answers1

1

I don't know why Stack Overflow has become like this!! You ask the question and about a hundred people see it without any attempt to help or comment!! This is very unfortunate. Anyway.

I fixed the issue by Re-rotate the image again manually:

Uri imageUri = data.getData();
        Bitmap bmp = null;
        try {
            bmp = MediaStore.Images.Media.getBitmap(getContentResolver(), imageUri);
        } catch (IOException e) {
            e.printStackTrace();
        }
        Matrix matrix = new Matrix();
        matrix.postRotate(90);
        Bitmap imageAfterRotation = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        imageAfterRotation.compress(Bitmap.CompressFormat.JPEG, 35, baos);
        byte[] datasa = baos.toByteArray();
Alex
  • 53
  • 5