How to get URL from Firebase Storage getDownloadURL

后端 未结 10 1905
小蘑菇
小蘑菇 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<UploadTask.TaskSnapshot>() {
                        @Override
                        public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
                            if (task.isSuccessful()) {
                                task.getResult().getMetadata().getReference().getDownloadUrl()
                                        .addOnCompleteListener(MainActivity.this, 
                                                new OnCompleteListener<Uri>() {
                                    @Override
                                    public void onComplete(@NonNull Task<Uri> 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());
                            }
                        }
                    });
        }
    
    0 讨论(0)
  • 2020-11-21 05:32

    here i am uploading and getting the image url at the same time...

               final StorageReference profileImageRef = FirebaseStorage.getInstance().getReference("profilepics/" + System.currentTimeMillis() + ".jpg");
    
                if (uriProfileImage != null) {
    
                profileImageRef.putFile(uriProfileImage)
                            .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                                @Override
                                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                                   // profileImageUrl taskSnapshot.getDownloadUrl().toString(); //this is depreciated
    
                              //this is the new way to do it
                       profileImageRef.getDownloadUrl().addOnCompleteListener(new OnCompleteListener<Uri>() {
                                        @Override
                                        public void onComplete(@NonNull Task<Uri> task) {
                                           String profileImageUrl=task.getResult().toString();
                                            Log.i("URL",profileImageUrl);
                                        }
                                    });
                                }
                            })
                            .addOnFailureListener(new OnFailureListener() {
                                @Override
                                public void onFailure(@NonNull Exception e) {
                                    progressBar.setVisibility(View.GONE);
                                    Toast.makeText(ProfileActivity.this, "aaa "+e.getMessage(), Toast.LENGTH_SHORT).show();
                                }
                            });
                }
    
    0 讨论(0)
  • 2020-11-21 05:33

    change the received URI to URL

     val urlTask = uploadTask.continueWith { task ->
                if (!task.isSuccessful) {
                    task.exception?.let {
                        throw it
                    }
                }
    
    
                spcaeRef.downloadUrl
            }.addOnCompleteListener { task ->
                if (task.isSuccessful) {
    
                    val downloadUri = task.result
    
                    //URL
                    val url = downloadUri!!.result
    
                } else {
                    //handle failure here
                }
            }
    
    
    0 讨论(0)
  • 2020-11-21 05:34

    Please refer to the documentation for getting a download URL.

    When you call getDownloadUrl(), the call is asynchronous and you must subscribe on a success callback to obtain the results:

    // Calls the server to securely obtain an unguessable download Url
    private void getUrlAsync (String date){
        // Points to the root reference
        StorageReference storageRef = FirebaseStorage.getInstance().getReference();
        StorageReference dateRef = storageRef.child("/" + date+ ".csv");
        dateRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>()
        {
            @Override
            public void onSuccess(Uri downloadUrl) 
            {                
               //do something with downloadurl
            } 
        });
    }
    

    This will return a public unguessable download url. If you just uploaded a file, this public url will be in the success callback of the upload (you do not need to call another async method after you've uploaded).

    However, if all you want is a String representation of the reference, you can just call .toString()

    // Returns a Uri of the form gs://bucket/path that can be used
    // in future calls to getReferenceFromUrl to perform additional
    // actions
    private String niceRefLink (String date){
        // Points to the root reference
        StorageReference storageRef = FirebaseStorage.getInstance().getReference();
        StorageReference dateRef = storageRef.child("/" + date+ ".csv");
        return dateRef.toString();
    }
    
    0 讨论(0)
  • 2020-11-21 05:36

    The getDownloadUrl method has now been depreciated in the new firebase update. Instead use the following method. taskSnapshot.getMetadata().getReference().getDownloadUrl().toString()

    0 讨论(0)
  • 2020-11-21 05:42

    For me, I did my code in Kotlin and I had the same error "getDownload()". Here are both the dependencies that worked for me and the Kotlin code.

    implementation 'com.google.firebase:firebase-storage:18.1.0' firebase storage dependencies

    This what I added and it worked for me in Kotlin. Storage() would come before Download()

    profileImageUri = taskSnapshot.storage.downloadUrl.toString()
    
    0 讨论(0)
提交回复
热议问题