java - 如何返回第二个值
问题描述
关于如何在此代码中返回第二个值的任何想法?
private void uploadImage_10() {
final ProgressDialog pd = new ProgressDialog(this);
pd.setMessage("Posting");
pd.show();
if (mImageUri != null&&mImageUri2 != null){
final StorageReference fileReference = storageRef.child(System.currentTimeMillis()
+ "." + getFileExtension(mImageUri));
final StorageReference fileReference2 = storageRef.child(System.currentTimeMillis()
+ "." + getFileExtension(mImageUri2));
uploadTask2 = fileReference2.putFile(mImageUri2);
uploadTask = fileReference.putFile(mImageUri);
uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
return fileReference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
miUrlOk = downloadUri.toString();
Uri downloadUri2 = task.getResult();
miUrlOk2 = downloadUri2.toString();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Posts");
String postid = reference.push().getKey();
HashMap<String, Object> hashMap = new HashMap<>();
hashMap.put("postid", postid);
hashMap.put("postimage", miUrlOk);
hashMap.put("postimage2", miUrlOk2);
hashMap.put("description", description.getText().toString());
hashMap.put("publisher", FirebaseAuth.getInstance().getCurrentUser().getUid());
我想返回 fileReference.getDownloadUrl(); 和 fileReference2.getDownloadUrl(); 但不知道如何。
解决方案
除非您在 List 中传递它们,否则您不能在一个方法中返回两个值。
当前的要求可以通过在单独的方法中传递 StorageReference 来实现,如下所示。
private void continueWithTask(StorageReference reference){
uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
return reference;
}
}
稍后像这样调用他们 continueWithTask(fileReference) & continueWithTask(fileReference2 )
推荐阅读
- python - ndarray.T 什么时候更新?
- java - 如何从 Android 广播 UDP 数据包?
- javascript - Stadia如何实现长按Escape键退出全屏?
- sass - 在 live sass 编译器之后,sftp 不上传编译文件
- android-management-api - 可能的错误 NOT_AVAILABLE_IN_COUNTRY
- javascript - 如何从另一个对象数组中过滤具有名称的对象数组
- powershell - Powershell 打开文件、编辑文件、更新文件 - 文档锁定问题
- itext - 使用 iTextSharp 嵌入 pdf 时图像被打乱
- html - 转换:在 Firefox 中翻译不精确?
- python - 您如何有效地提取 Amazon Kinesis Video Stream 以进行自定义 python 处理?