Skip to content
Advertisement

How to update the the bitmap image inside the FirebaseStorage in android without changing its downloadUrl in the realtimedatabase

I want to update the bitmap image inside the URL without changing its download URL in the real-time database because at uploading for the first time I have uploaded the image in the storage and stored it URL in the real time database now I want to update that image how should I approach this ??

    package com.parth.iitktimes;
    
    import androidx.activity.result.ActivityResultCallback;
    import androidx.activity.result.ActivityResultLauncher;
    import androidx.activity.result.contract.ActivityResultContracts;
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.app.ProgressDialog;
    import android.graphics.Bitmap;
    import android.graphics.drawable.Drawable;
    import android.net.Uri;
    import android.os.Bundle;
    import android.provider.MediaStore;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;
    
    import com.google.android.material.card.MaterialCardView;
    import com.google.firebase.database.DatabaseReference;
    import com.google.firebase.database.FirebaseDatabase;
    import com.squareup.picasso.Picasso;
    import com.squareup.picasso.RequestCreator;
    import com.squareup.picasso.Target;
    
    import java.io.IOException;
    import java.io.Serializable;
    
    import de.hdodenhof.circleimageview.CircleImageView;
    
    public class updataDelete extends AppCompatActivity {
        //declaring private variables
        private MaterialCardView SelectImage;
        private EditText edCreatorName, edCreatorDesignation, edCreatorEmail, edCreatorMobile;
        private Button btnUpdateCreator,btnDeleteCreator;
        private CircleImageView previewImage;
        private Bitmap mbitmap;
        private ActivityResultLauncher<String> someActivityResultLauncher;
    
        //download url of the uploaded images
        private String downloadUrl = "";
    
        //progressDialog for updates
        private ProgressDialog progressDialog;
    
        private Creator obj;
    
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_updata_delete);
    
            //giving context to the progress dialog
            progressDialog = new ProgressDialog(this);
    
            // initialisation
            SelectImage = findViewById(R.id.select_image);
            edCreatorName = findViewById(R.id.edCreatorName);
            edCreatorDesignation = findViewById(R.id.edCreatorDesignation);
            edCreatorEmail = findViewById(R.id.edCreatorEmail);
            edCreatorMobile = findViewById(R.id.edCreatorMobile);
            btnUpdateCreator = findViewById(R.id.updateCreator);
            btnDeleteCreator = findViewById(R.id.deleteCreator);
            previewImage = findViewById(R.id.preview_image);
    
            progressDialog = new ProgressDialog(this);
    
            //accessing the object from the intent as a serializable extra
            obj = (Creator) getIntent().getSerializableExtra("model object");
    
            edCreatorName.setText(obj.getName());
            edCreatorDesignation.setText(obj.getDesign());
            edCreatorEmail.setText(obj.getEmail());
            edCreatorMobile.setText(obj.getPhone());
    
            edCreatorName.requestFocus();
    
    
    
            //loading the image from the preview data url to the bitmap variable and preview image
             Target target = new Target() {
                @Override
                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                    //initializing the variable and preview image source
                    mbitmap = bitmap;
                    previewImage.setImageBitmap(mbitmap);
    
                }
    
                @Override
                public void onBitmapFailed(Exception e, Drawable errorDrawable) {
                    Toast.makeText(getApplicationContext(),"couldnt load bitmap",Toast.LENGTH_SHORT).show();
                }
    
                @Override
                public void onPrepareLoad(Drawable placeHolderDrawable) {
    
                }
            };
            Picasso.get().load(obj.getDownloadUrl()).into(target);
    
            //click listener for upload button
            btnUpdateCreator.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String Name = edCreatorName.getText().toString();
                    String Email = edCreatorEmail.getText().toString();
                    String Phone = edCreatorMobile.getText().toString();
                    String Post = edCreatorDesignation.getText().toString();
    
                    if (Name.isEmpty()) {
                        edCreatorName.setError("*required");
                    } else if (Email.isEmpty()) {
                        edCreatorEmail.setError("*required");
                    } else if (Phone.isEmpty()) {
                        edCreatorMobile.setError("*required");
                    } else if (Post.isEmpty()) {
                        edCreatorDesignation.setError("*required");
                    } else if (mbitmap== null) {
                        Toast.makeText(getApplicationContext(), "Please add an image", Toast.LENGTH_SHORT).show();
                    } else {
                        updateMember();
                    }
                }
            });
    
            //activity result launcher
            someActivityResultLauncher = registerForActivityResult(
                    new ActivityResultContracts.GetContent(), new ActivityResultCallback<Uri>() {
                        @Override
                        public void onActivityResult(Uri result) {
                            try {
                                mbitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), result);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            previewImage.setImageBitmap(mbitmap);
                        }
                    });
    
    
            //select image click listener
            SelectImage.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    someActivityResultLauncher.launch("image/*");
                }
            });
    
            //delete btn click event
            btnDeleteCreator.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(updataDelete.this, "deleted successfully", Toast.LENGTH_SHORT).show();
                }
            });
        }
    
        private void updateMember(){
            Toast.makeText(updataDelete.this,"updated successfully",Toast.LENGTH_SHORT).show();
         //inside here I want to update the bitmap image inside the URL without changing its download URL in the real-time database because at uploading for the first time I have uploaded the image in the storage and stored it URL in the real-time database now I want to update that image how should //I approach this ??
        }
    }

above written code was the activity that I used to update the image bitmap

Advertisement

Answer

It’s not possible. Each version of a file uploaded to storage has a unique URL. If the object in storage is changed by uploading a new version, then it will require a new URL (with a new token) to get the latest version.

See also: Firebase Storage – Download URL different from actual Download URL in the console (token differs)

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement