How to get URL from Firebase Storage getDownloadURL

后端 未结 10 1992
小蘑菇
小蘑菇 2020-11-21 04:54

I\'m trying to get the \"long term persistent download link\" to files in our Firebase storage bucket. I\'ve changed the permissions of this to

service fireb         


        
10条回答
  •  感动是毒
    2020-11-21 05:30

    Clean And Simple
    private void putImageInStorage(StorageReference storageReference, Uri uri, final String key) {
            storageReference.putFile(uri).addOnCompleteListener(MainActivity.this,
                    new OnCompleteListener() {
                        @Override
                        public void onComplete(@NonNull Task task) {
                            if (task.isSuccessful()) {
                                task.getResult().getMetadata().getReference().getDownloadUrl()
                                        .addOnCompleteListener(MainActivity.this, 
                                                new OnCompleteListener() {
                                    @Override
                                    public void onComplete(@NonNull Task task) {
                                        if (task.isSuccessful()) {
                                            FriendlyMessage friendlyMessage =
                                                    new FriendlyMessage(null, mUsername, mPhotoUrl,
                                                            task.getResult().toString());
                                            mFirebaseDatabaseReference.child(MESSAGES_CHILD).child(key)
                                                    .setValue(friendlyMessage);
                                        }
                                    }
                                });
                            } else {
                                Log.w(TAG, "Image upload task was not successful.",
                                        task.getException());
                            }
                        }
                    });
        }
    

提交回复
热议问题