Uri第二次在调用函数时返回下载URL,并在首次Firebase Storage时返回null
值。
private void uploadFile() {
progressDialog.show();
if (mUploadTask != null && mUploadTask.isInProgress()) {
Toast.makeText(MainActivity.this, "Upload in progress", Toast.LENGTH_SHORT).show();
} else {
if (mImageUri != null) {
final StorageReference fileReference = mStorageRef.child(System.currentTimeMillis()
+ "." + getFileExtension(mImageUri));
mUploadTask = fileReference.putFile(mImageUri)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(MainActivity.this, "Upload successful", Toast.LENGTH_LONG).show();
fileReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Uri download = uri;
downloadUrl = download.toString();
}
});
Log.e("uploaded", "AA " + downloadUrl);
// String uploadId = mDatabaseRef.push().getKey();
//mDatabaseRef.child(uploadId).setValue(upload);
Toast.makeText(MainActivity.this, "downloadURi" + downloadUrl, Toast.LENGTH_LONG).show();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
progressDialog.dismiss();
} else {
progressDialog.dismiss();
Toast.makeText(this, "No file selected", Toast.LENGTH_SHORT).show();
}
}
progressDialog.dismiss();
}