同一文件上传到firebase

时间:2018-04-20 12:27:11

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

我要将文件插入firebase存储中的两个引用,并添加名称并将文件的Url下载到firebase数据库中的其他两个引用。文件按预期上传到firebase存储,但名称和下载Url没有按预期存储在数据库中。而不是将一对名称和下载Url添加到数据库中的每个引用,而是将两个对添加到同一引用中。

case R.id.facultySpaceStudentRadioButton :
             if(validInputs(fileChoosed)){
                 sendFile("ASSIGNMENT");
             }
             break;

        case R.id.facultySpaceParentRadioButton :
            if(validInputs(fileChoosed)){
                sendFile("PERFORMANCE");
            }
            break;

        case R.id.facultySpaceBothRadioButton :
            if(validInputs(fileChoosed)){
                sendFile("ASSIGNMENT");
                sendFile("PERFORMANCE");
            }
            break;

        default :
            if(fileChoosed == null){
                mProgressDialog.dismiss();
                fileNotChoosed();
            } else {
                mProgressDialog.dismiss();
                AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this)
                        .setCancelable(false).setNeutralButton("OK", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        }).setTitle("Alert !").setMessage("Please choose a receiver !");
                AlertDialog alert = alertBuilder.create();
                alert.show();
            }
    }

}

private void sendFile(String recipient){
    mDatabaseReference = FirebaseDatabase.getInstance().getReference(recipient)
            .child(facultySpaceStream).child(facultySpaceBatchEditText.getText().toString());
    mStorageReference = FirebaseStorage.getInstance().getReference();
    mStorageReference.child(recipient + "/").child(facultySpaceStream + "/")
            .child(facultySpaceBatchEditText.getText().toString() + "/" + fileChoosed.getLastPathSegment())
            .putFile(fileChoosed)
            .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                    mProgressDialog.dismiss();
                    Upload upload = new Upload(facultySpaceFileNameEditText.getText().toString(), taskSnapshot.getDownloadUrl().toString());
                    mDatabaseReference.child(mDatabaseReference.push().getKey()).setValue(upload);
                    resetFields();
                    Toast.makeText(FacultySpace.this, "File sent successfully.", Toast.LENGTH_SHORT).show();
                }
            })
            .addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    mProgressDialog.dismiss();
                    facultySpaceFileNameEditText.setText("");
                    Toast.makeText(FacultySpace.this, "Sending Failed.", Toast.LENGTH_SHORT).show();
                }
            })
            .addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                    double progress = ((100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount());
                    mProgressDialog.setMessage(new DecimalFormat("##.#").format(progress) + "% Sending File...");
                }
            });
} 

正如您所看到的,我正在使用切换机箱,第一种和第二种情况正如预期的那样完美运行,但是当执行第三种情况时,数据库会收到不正确的值。

0 个答案:

没有答案