How to get URL from Firebase Storage getDownloadURL

后端 未结 10 1995
小蘑菇
小蘑菇 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:48

    The getDownloadUrl method was removed in firebase versions greater than 11.0.5 I recommend using version 11.0.2 that still uses this method.

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

    //Firebase Storage - Easy to Working with uploads and downloads.

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == RC_SIGN_IN){
            if(resultCode == RESULT_OK){
                Toast.makeText(this,"Signed in!", LENGTH_SHORT).show();
            } else if(resultCode == RESULT_CANCELED){
                Toast.makeText(this,"Signed in canceled!", LENGTH_SHORT).show();
                finish();
            }
        } else if(requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK){
    
            // HERE I CALLED THAT METHOD
            uploadPhotoInFirebase(data);
    
        }
    }
    
    private void uploadPhotoInFirebase(@Nullable Intent data) {
        Uri selectedImageUri = data.getData();
    
        // Get a reference to store file at chat_photos/<FILENAME>
        final StorageReference photoRef = mChatPhotoStorageReference
                        .child(selectedImageUri
                        .getLastPathSegment());
    
        // Upload file to Firebase Storage
        photoRef.putFile(selectedImageUri)
                .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
    
                        // Download file From Firebase Storage
                        photoRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                            @Override
                            public void onSuccess(Uri downloadPhotoUrl) {
                                //Now play with downloadPhotoUrl
                                //Store data into Firebase Realtime Database
                                FriendlyMessage friendlyMessage = new FriendlyMessage
                                        (null, mUsername, downloadPhotoUrl.toString());
                                mDatabaseReference.push().setValue(friendlyMessage);
                            }
                        });
                    }
                });
    }
    
    0 讨论(0)
  • 2020-11-21 05:52

    You can Upload images to firestore and get it's download URL as below function:

      Future<String> uploadPic(File _image) async {
    
        String fileName = basename(_image.path);
        StorageReference firebaseStorageRef = FirebaseStorage.instance.ref().child(fileName);
        StorageUploadTask uploadTask = firebaseStorageRef.putFile(_image);
        var downloadURL = await(await uploadTask.onComplete).ref.getDownloadURL();
        var url =downloadURL.toString();
    
       return url;
    
      }
    
    0 讨论(0)
  • 2020-11-21 05:53
    StorageReference mStorageRef = FirebaseStorage.getInstance().getReference();
    
    final   StorageReference fileupload=mStorageRef.child("Photos").child(fileUri.getLastPathSegment());
    UploadTask uploadTask = fileupload.putFile(fileUri);
    
    Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
        @Override
        public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
            if (!task.isSuccessful()) {
                throw task.getException();
            }
            return ref.getDownloadUrl();
    
        }
        }).addOnCompleteListener(new OnCompleteListener<Uri>() {
            @Override
            public void onComplete(@NonNull Task<Uri> task) {
                if (task.isSuccessful()) {
                    Uri downloadUri = task.getResult();
                    Picasso.get().load(downloadUri.toString()).into(image);
    
                } else {
                     // Handle failures
                }
           }
    });
    
    0 讨论(0)
提交回复
热议问题