uri在第二次调用该函数时返回下载URL,并在首次存储Firebase时返回空值

时间:2018-11-06 10:47:12

标签: java android firebase firebase-storage

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();
}

0 个答案:

没有答案