各位大大你們好!小弟我目前遇到一個問題是
``
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_File_REQUEST) {
dialog.show();
FilePath = data.getData();
UploadTask UploadTask = mStorageRef.putFile(FilePath);
Task task = UploadTask.continueWithTask(new Continuation<com.google.firebase.storage.UploadTask.TaskSnapshot, Task>() {
@Override
public Task then(@NonNull Task<com.google.firebase.storage.UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
Toast.makeText(UploadAndDownload.this, "上傳失敗", Toast.LENGTH_SHORT).show();
}
return mStorageRef.getDownloadUrl();
}
})
.addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
String fileId = mDatabaseRef.push().getKey();
String url = task.getResult().toString();
Upload upload = new Upload(fileId, editFileName.getText().toString(), url);
mDatabaseRef.child(fileId).setValue(upload);
Toast.makeText(UploadAndDownload.this, "上傳成功", Toast.LENGTH_SHORT).show();
dialog.dismiss();
editFileName.setText(null);
}
});
}
}
``
上面這段上傳 在UploadTask UploadTask = mStorageRef.putFile(FilePath);
我想要加上.child(editFileName.getText().toString()) 讓上傳的檔案可以依照我輸入的去命名 但是只要加上child以後就會不行想請問是什麼原因呢?
android新手不太懂感謝回復
以下是error
at com.google.android.gms.tasks.zzu.getResult(Unknown Source)
at com.example.myapplication.UploadAndDownload$5.onComplete(UploadAndDownload.java:177)
似乎是加上了Child就會抓不到urlQQ想問一下我怎麼改會比較好呢?
完整的程式碼 https://github.com/ppp753951555/question/issues/1
2020/05/13
結果我換了另一種方式是再寫一個上傳的method然後再用
在上傳的地方這樣寫
UploadTask uploadTask = ref.putFile(FilePath); uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { @Override public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { Task<Uri> urlTask = taskSnapshot.getStorage().getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() { @Override //get upload url public void onSuccess(Uri uri) { String url = uri.toString(); } }); } });
希望能給後面的人參考到~