英文:
How can a wait function that uploads an image to firestore storage finish before running another function in android java?
问题
以下是已翻译的内容:
我想要制作一个应用程序,通过在Android中使用Java,将许多图像上传到Firestore存储,并获取所有这些图像的URL,并将其发布到Firestore数据库中。
我有这个用于在数据库中发布的函数:
public void uploadFile(Map<String, Object> item, String phone) {
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("Users").document().collection(phone).add(item)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Toast.makeText(getApplicationContext(), "上传完成", Toast.LENGTH_LONG).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
}
数据是我要发布到数据库中的数据哈希表。此数据包含以下信息:
Map<String, Object> data = new HashMap<>();
data.put("name", name);
data.put("phone", phone);
data.put("city", city);
data.put("itemName", itemName);
data.put("price", price);
data.put("type", type);
data.put("subtype", subtype);
data.put("cond", cond);
data.put("details", details);
我有这个用于上传照片的函数:
public void upLoadImage(File data, Map<String, Object> list, int KEY) {
Uri file = Uri.fromFile(data);
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReference().child("images/" + file.getLastPathSegment());
UploadTask uploadTask = storageRef.putFile(file);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
url = "失败:" + e.getMessage();
}
});
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
if (KEY == 1) {
list.put("image1", String.valueOf(uri));
} else if (KEY == 2) {
list.put("image2", String.valueOf(uri));
} else if (KEY == 3) {
list.put("image3", String.valueOf(uri));
}
}
});
}
});
}
此函数接受两个参数:
- 一个列表,将下载URL推送到其中。
- 图像文件。
我想要像这样运行它:
uploadImage(path1, data, 1);
uploadImage(path2, data, 2);
uploadImage(path3, data, 3);
uploadFile();
这意味着我想要上传图像并等待上传完成,然后上传第二个图像并等待上传完成,依此类推,最后运行uploadFile()
,因为我的问题是uploadFile()
不会等待图像上传完成。
英文:
I want to make app upload many images to firestore storage and get all of the urls of those images and post it in firestore database by using java in android.
I have this function to post on database
public void uploadFile(Map<String,Object> item , String phone){
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection("Users").document().collection(phone).add(item)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Toast.makeText(getApplicationContext(),"Upload Complete",Toast.LENGTH_LONG).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
}
The data is a hashmap of data I will post to the database. This data contains this information:
Map<String,Object> data = new HashMap<>();
data.put("name" , name);
data.put("phone" , phone);
data.put("city" , city);
data.put("itemName" , itemName);
data.put("price" , price);
data.put("type" , type);
data.put("subtype" , subtype);
data.put("cond" , cond);
data.put("details" , details);
and I have this function to upload photo
public void upLoadImage(File data,Map<String,Object> list,int KEY){
Uri file = Uri.fromFile(data);
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReference().child("images/"+file.getLastPathSegment());
UploadTask uploadTask = storageRef.putFile(file);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
url = "fails " + e.getMessage();
}
});
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
if(KEY == 1){
list.put("image1",String.valueOf(uri));
}else if(KEY ==2){
list.put("image2",String.valueOf(uri));
}else if(KEY == 3){
list.put("image3",String.valueOf(uri));
}
}
});
}
});
this function take two argument data:
- list that will push download url inside it
- image file
I want to run it just like this
uploadImage(path1,data,1);
uploadImage(path2,data,2);
uploadImage(path3,data,3);
uploadfile()
This means that I want to upload image and complete then upload sec image and complete etc., then run uploadfile because my problem that uploadfile not wait upload image to complete.
答案1
得分: 0
以下是翻译好的部分:
public void upLoadImage(String path, String path2, String path3, Map<String, Object> list, int KEY, String Auth) {
if (!path.equals("empty") && path != null) {
Toast.makeText(getApplicationContext(), "path = " + path, Toast.LENGTH_LONG).show();
Uri file = Uri.fromFile(new File(path));
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReference().child("images/" + file.getLastPathSegment());
UploadTask uploadTask = storageRef.putFile(file);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(), "No Connection", Toast.LENGTH_LONG).show();
}
});
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
if (KEY == 1) {
list.put("image1", String.valueOf(uri));
upLoadImage(path2, "null", path3, list, 2, Auth);
}
if (KEY == 2) {
list.put("image2", String.valueOf(uri));
upLoadImage(path3, "null", "null", list, 3, Auth);
}
if (KEY == 3) {
list.put("image3", String.valueOf(uri));
uploadFile(list, Auth);
}
}
});
}
});
} else {
uploadFile(list, Auth);
}
}
英文:
The problem was resolved by write upload image like this
public void upLoadImage(String path,String path2, String path3,Map<String,Object> list,int KEY,String Auth){
if(!path.equals("empty") && path != null){
Toast.makeText(getApplicationContext(),"path = " + path,Toast.LENGTH_LONG).show();
Uri file = Uri.fromFile(new File(path));
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReference().child("images/"+file.getLastPathSegment());
UploadTask uploadTask = storageRef.putFile(file);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getApplicationContext(),"No Connection",Toast.LENGTH_LONG).show();
}
});
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
if(KEY == 1 ){
list.put("image1",String.valueOf(uri));
upLoadImage(path2,"null",path3,list,2,Auth);
}
if(KEY == 2){
list.put("image2",String.valueOf(uri));
upLoadImage(path3,"null","null",list,3,Auth);
}
if(KEY == 3){
list.put("image3",String.valueOf(uri));
uploadFile(list,Auth);
}
}
});
}
});
}else {
uploadFile(list,Auth);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论