我正在尝试将自定义对象上载到Firebase Firestore,但是Firestore对象应包含下载图像网址。我要做的是先上传图像,等待图像完成,获取对下载URL的引用,更新我的自定义类,然后将其上传到Firestore。然后,我想在最后一次上传(上传到Firestore)完成时通知我的视图。我当时正在考虑通过返回任务来做到这一点。这是我要上传的图片:
ref.putFile(file).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
//use uri to update object and then upload to firestore
mObject.setImageUri(uri)
uploadToFirestore(myObject);
}
Log.d(TAG, "onSuccess: uri= "+ uri.toString());
}
});
}
});`
我不确定如何返回Firstore上传的最终任务,因为它是在图像上传任务中完成的。我的目标是侦听uploadToFireStore()何时完成,然后通知我的视图。任何建议,将不胜感激。谢谢!
编辑:我的结构像这样- 视图已从用户获取信息,并将其作为自定义对象传递给viewmodel。然后,视图模型调用db进行上传。 我的想法是从数据库返回一个任务到viewmodel,这将返回所述任务到视图。查看是否完成检查,然后停止加载程序。这是思考这个问题的好方法吗?
答案 0 :(得分:1)
我的工作示例。
public class SomeClass {
private String downloadUrl;
private final FirebaseStorage storage = FirebaseStorage.getInstance();
private int count = 0;
public interface Callback {
void callingBack();
void getReady();
}
public int getCount() {
return count;
}
private Callback callback;
public void registerCallBack(Callback callback) {
this.callback = callback;
}
public String getImageUri () {
return downloadUrl;
}
public void uploadImage (String srcPath, String fileName, Context context, int size) {
StorageReference storageRef = storage.getReference();
StorageReference imageRef = storageRef.child(fileName);
try {
InputStream inputStream = new FileInputStream(new File(srcPath));
imageRef.putStream(inputStream)
.continueWithTask(task -> imageRef.getDownloadUrl())
.addOnCompleteListener(task -> {
downloadUrl = task.getResult().toString();
callback.callingBack();
this.count++;
if (this.count == size) {
callback.getReady();
this.count = 0;
}
});
} catch (Exception e) {
Toast.makeText(context, "Connection error", Toast.LENGTH_LONG).show();
}
}
}
活动代码
public void someMehtod() {
String fileName;
String filePath;
int size = YOUR_SIZE;
String[] imgUrlList = new String[size];
for (int i = 0; i < size; i++) {
fileName = "img_" + i + ".jpg"; // set target file name
filePath = "/storage/emulated/0/Download/images_" + i + ".jpg"; // for example (u need get your real source file paths)
getImageUri.registerCallBack(new GetImageUri.Callback() {
@Override
public void callingBack() {
imgUrlList [getImageUri.getCount()] = getImageUri.getImageUri();
}
}
@Override
public void getReady() {
// here u can use your URLs
for (int k = 0; k < size; k ++) {
Log.d(TAG, "getReady: " + imgUrlList [k]);
}
});
getImageUri.uploadImage(filePath, fileName, this.getContext(), imgListCount);
}
}
答案 1 :(得分:0)
我想出了解决问题的方法。使用回调。像这样创建一个界面
public interface Callback {
void result(Result result);
void url(Uri downloadUrl);
}
注意:结果是具有两个值FAILED和SUCCESS的枚举类 然后在您的视图中像这样调用视图模型
ViewModel.saveoBJECT(mObject, new Callback() {
@Override
public void result(Result result) {
switch (result){
case SUCCESS:
mProgressbar.setVisibility(View.GONE);
break;
case FAILED:
Log.v(TAG, "Failed");
Toast.makeText(getApplicationContext(), "OOPS ERROR!", Toast.LENGTH_LONG).show();
break;
}
}
@Override
public void url(Uri downloadUrl) { }
});
在您的数据库中执行以下操作
uploadImage(Uri.parse(imageString), prepend, new Callback() {
@Override
public void result(Result result) {}
@Override
public void uri(Uri downloadUrl) {
saveObjectInDB(downloadUrl, new Callback() {
@Override
public void result(Result result) {
callback.result(result);
}
@Override
public void url(Uri downloadUrl) { }
});
}
});
在上传图片功能中
ref.putFile(file).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
callback.journey(uri);
Log.d(TAG, "onSuccess: uri= "+ uri.toString());
}
});
}
});
并在saveObjectInDB函数中执行此操作
db.collection(DATABASE).add(O)
.addOnSuccessListener(new OnSuccessListener<DocumentReference>() {
@Override
public void onSuccess(DocumentReference documentReference) {
Log.w(TAG, "Document upload complete");
callback.result(Result.SUCCESS);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.w(TAG, "Error adding uploading document", e);
callback.result(Result.FAILED);
}
});