如何在不更改实时数据库中的 downloadUrl 的情况下更新 android 中 Firebase 存储中的位图图像

Posted

技术标签:

【中文标题】如何在不更改实时数据库中的 downloadUrl 的情况下更新 android 中 Firebase 存储中的位图图像【英文标题】:How to update the the bitmap image inside the FirebaseStorage in android without changing its downloadUrl in the realtimedatabase 【发布时间】:2021-10-05 17:22:33 【问题描述】:

我想更新 URL 中的位图图像而不更改其在实时数据库中的下载 URL,因为在第一次上传时,我已将图像上传到存储中并将其 URL 存储在实时数据库中想要更新该图像我应该如何处理这个??

    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 ??
        
    

上面写的代码是我用来更新图像位图的活动

【问题讨论】:

【参考方案1】:

这是不可能的。上传到存储的文件的每个版本都有一个唯一的 URL。如果通过上传新版本更改存储中的对象,则需要一个新 URL(带有新令牌)才能获取最新版本。

另请参阅:Firebase Storage - Download URL different from actual Download URL in the console (token differs)

【讨论】:

所以我必须删除旧文件然后添加一个新文件? 不,您只需要为更改的对象请求一个新的下载 URL。您可以根据需要删除旧的,但您必须确定这样做是否有好处。

以上是关于如何在不更改实时数据库中的 downloadUrl 的情况下更新 android 中 Firebase 存储中的位图图像的主要内容,如果未能解决你的问题,请参考以下文章

如何让 GraphQL 在不进行轮询的情况下从数据库中获取实时/新数据?

如何在实时数据库firebase android中存储下载图像url

如何在不更改特定列的情况下对数据框中的数据进行重新采样?

如何在不更改特定列的情况下对数据框中的数据进行重新采样?

如何在不更改原始列表的情况下更改新列表?

如何在不重新加载页面的情况下更新从 firestore 检索到的标记在我的地图上?