DownloadUrl for firebase storage not working [dupl

2019-08-28 09:44发布

This question already has an answer here:

I tried finding on a lot of stackoverflow questions, and many other places, but could not find a working method.

I am uploading a picture to firebase storage, and on upload I want a download link for that picture/document.

taskSnapshot.getDownloadUrl() is now deprecated, so they have mentioned to use getDownloadUrl on the Firebase Storage Reference, or getResult on the taskSnapshot, but both return some jargon and I am not able to get a proper download url.

btnUploadImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!ImageUri.toString().trim().equals(""))
                {

                    final StorageReference imageRef = mFirebaseStorage.child("some.jpg");
                    imageRef.putFile(ImageUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
                            Toast.makeText(MainActivity.this, "Done", Toast.LENGTH_SHORT).show();
                            Log.d("UploadSuccess", ""+task.getResult()+"\n"+mFirebaseStorage.getDownloadUrl());
                        }
                    });

                }else
                {
                    Toast.makeText(MainActivity.this, "Please Select An Image.", Toast.LENGTH_SHORT).show();
                }
            }
        });

this gave me an log Output :

D/UploadSuccess: com.google.firebase.storage.UploadTask$TaskSnapshot@fff6f13 com.google.android.gms.tasks.zzu@c781349

and the version of firebase/storage i am using :

implementation 'com.google.firebase:firebase-database:16.1.0'
implementation 'com.google.firebase:firebase-storage:16.1.0'

1条回答
The star\"
2楼-- · 2019-08-28 10:14

Try this

val ref = mStorageReference?.child("some.jpg")
        val  uploadTask = ref?.putFile(Uri.fromFile(File(mImagePath)))

        uploadTask?.continueWithTask(Continuation<UploadTask.TaskSnapshot, Task<Uri>>
        { task ->
            if (!task.isSuccessful) {
                task.exception?.let {
                    throw it
                }
            }
            return@Continuation ref.downloadUrl
        })?.addOnCompleteListener { task ->
            if (task.isSuccessful) {
                val downloadUri = task.result
                mTempDatabaseReference?.child("image")?.setValue(downloadUri.toString())
            } else {
                // Handle failures
                // ...
            }
        }
查看更多
登录 后发表回答