gpt4 book ai didi

java - 将图像上传到 Firebase 存储后,无法将图像 url 放入 Firestore 数据库

转载 作者:行者123 更新时间:2023-12-02 00:39:00 25 4
gpt4 key购买 nike

我正在尝试将一些图像上传到 firestore 存储并将图像 URL 放入 firebase 数据库。图像上传成功,但图像 URL 未添加到数据库中。我认为可能导致此问题的问题是,在上传图像后将图像 URL 添加到 Hashmap,但数据库上传过程不会等待 URL,而是在上传任务之前将所有其他 HashMap 键添加到数据库返回 URL。这样,所有其他键都会添加到数据库中,但图像 URL 不会。在下面的代码中,产品 ID 已成功添加到数据库中,如果我未选择任何图像,其 url 也会作为空添加到数据库中,工作正常,但如果我选择要上传的图像,则 HashMap 到数据库上传完成甚至在获取上传的图像 URL 之前。

public class AddProductDataActivity extends AppCompatActivity {

String productId;
EditText productIdEditText;
ImageView addProductImage3;

Button addProductSubmit;

final int IMAGE3_REQUEST = 30;

Uri image3LocationPath;

StorageReference objectStorageReference;
FirebaseFirestore objectFireBaseFireStore;

Map<String, String> objectMap = new HashMap<>();
StorageReference img3Store;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_product_data);

brandNameEditText =(EditText)
addProductImage3 = (ImageView) findViewById(R.id.add_product_image3);


objectStorageReference =
FirebaseStorage.getInstance().getReference("images");
objectFireBaseFireStore = FirebaseFirestore.getInstance();


addProductImage3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

Intent objectIntent = new Intent();
objectIntent.setType("image/*");

objectIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(objectIntent, IMAGE3_REQUEST);
}
});


addProductSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
productId = productIdEditText.getText().toString();

if(image3LocationPath != null)
{
final String image3Name = productId + "_image3." + getExtension(image3LocationPath);
img3Store = objectStorageReference.child(image3Name);
UploadTask imageUploadTask = img3Store.putFile(image3LocationPath);
imageUploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if(!task.isSuccessful())
{
Toast.makeText(AddProductDataActivity.this, "Task Unsuccessful", Toast.LENGTH_SHORT).show();
}
return img3Store.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if(task.isSuccessful())
{
String image_3_url = task.getResult().toString();
objectMap.put("image3_url",image_3_url);
}
else
{
Toast.makeText(AddProductDataActivity.this, task.getException().toString(), Toast.LENGTH_SHORT).show();
}
}
});
}
else
{
objectMap.put("image3_url","");
}


objectFireBaseFireStore.collection("images").document(productId).set(objectMap).addOnSuccessListener(new OnSuccessListener<Void>() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(AddProductDataActivity.this, "Product Added Successfully.", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(AddProductDataActivity.this, "Error in Adding Product. Please Try Again.\n"+e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}

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

case 30:

try
{
if(resultCode == RESULT_OK && data != null && data.getData() != null)
{
image3LocationPath = data.getData();
Bitmap objectBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), image3LocationPath);

addProductImage3.setImageBitmap(objectBitmap);
}
}
catch (Exception e)
{
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
break;

default:
break;

}

private String getExtension(Uri uri){
try
{
ContentResolver objectContentResolver = getContentResolver();
MimeTypeMap objectMimeTypeMap = MimeTypeMap.getSingleton();
return objectMimeTypeMap.getExtensionFromMimeType(objectContentResolver.getType(uri));
}
catch (Exception e)
{
Toast.makeText(AddProductDataActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
return null;
}
}
}

最佳答案

你的代码

objectFireBaseFireStore.collection("images").document(productId).set(objectMap)

先执行

objectMap.put("image3_url",image_3_url)

这意味着当您设置 url 时,您的 objectMap 没有键 image3_url

尝试在 onComplete() 内设置数据库值或使用不同的解决方法仅设置 url。

关于java - 将图像上传到 Firebase 存储后,无法将图像 url 放入 Firestore 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57956231/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com