如何返回第二个值

时间:2019-09-05 16:22:52

标签: java android firebase firebase-realtime-database firebase-storage

关于如何在此代码中返回第二个值的任何想法?

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();但不知道如何。

1 个答案:

答案 0 :(得分:0)

除非在列表中传递它们,否则不能在单个方法中返回两个值。

可以通过以下类似的单独方法传递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;
            }
        }

此后再称它们为continuWithTask(fileReference)和continueWithTask(fileReference2)