从手机上传PDF到Firebase

时间:2019-09-08 11:33:38

标签: java android firebase firebase-storage

因此,我正在为一个项目编写代码,用户可以在其中上传PDF格式的问题和答案,到目前为止,我已经完成的代码会打开文件管理器,但是当我选择文件时,它将关闭而没有任何响应。

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {
        uploadFile(data.getData());
    }
}

private void uploadFile(Uri data) {
    final ProgressDialog progressDialog = new ProgressDialog(this);
    progressDialog.setTitle("Loading...");
    progressDialog.show();

    StorageReference reference = storageRef.child("UploadPaper/PastPaper/PDF" + System.currentTimeMillis() + ".pdf");
    reference.putFile(data)
        .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                Task<Uri> uri = taskSnapshot.getStorage().getDownloadUrl();
                while(!uri.isComplete());
                Uri url = uri.getResult();

                paperUpload = new PaperUpload(pdfName.getText().toString(), url.toString());
                dbRef.child(dbRef.push().getKey()).setValue(paperUpload);
                Toast.makeText(UploadOrEdit.this, "Uploaded", Toast.LENGTH_SHORT).show();
                progressDialog.dismiss();
            }
        }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
                double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot.getTotalByteCount());
                progressDialog.setMessage("Uploaded: " +(int) progress + "%");
            }
        });
}

这是我在文件管理器上单击文件时的日志猫输出:

W/InputMethodManager: startInputInner : InputBindResult == null need restart
W/IInputConnectionWrapper: showStatusIcon on inactive InputConnection
V/FA: Activity resumed, time: 8459656

1 个答案:

答案 0 :(得分:0)

找到了一个可以帮助您解决问题的链接:

PyOS_double_to_string