我想更新 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 ??
}
}
上面写的代码是我用来更新图像位图的活动
答案 0 :(得分:0)
这不可能。上传到存储的文件的每个版本都有一个唯一的 URL。如果通过上传新版本更改了存储中的对象,则需要一个新 URL(带有新令牌)才能获取最新版本。
另见:Firebase Storage - Download URL different from actual Download URL in the console (token differs)