Search code examples
javaandroidfirebasefirebase-storageandroid-bitmap

Bitmap rotate image 90 degrees after uploading to Firebase


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

Solution

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