1

I am uploading images from device storage to Firebase Storage using this:

final StorageReference fileReference = storageReference.child(System.currentTimeMillis()
                    +"."+getFileExtension(imageUri));

            uploadTask = fileReference.putFile(imageUri);

But now I need to upload a picture from a URL. I have the picture URL and need to put that picture in Firebase Storage. I haven´t found any resources that explain this feature:

EDIT. Complete code for uploading files from device´s storage to Firebase Storage:

private void uploadImage(){
        final ProgressDialog pd = new ProgressDialog(getContext());
        pd.setMessage("Uploading");
        pd.show();

        if (imageUri != null){
            final StorageReference fileReference = storageReference.child(System.currentTimeMillis()
                    +"."+getFileExtension(imageUri));

            uploadTask = fileReference.putFile(imageUri);
            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  fileReference.getDownloadUrl();
                }
            }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                @Override
                public void onComplete(@NonNull Task<Uri> task) {
                    if (task.isSuccessful()){
                        Uri downloadUri = task.getResult();
                        String mUri = downloadUri.toString();

                        reference = FirebaseDatabase.getInstance().getReference("Users").child(fuser.getUid());
                        HashMap<String, Object> map = new HashMap<>();
                        map.put("imageURL", ""+mUri);
                        reference.updateChildren(map);

                        pd.dismiss();
                    } else {
                        Toast.makeText(getContext(), "Failed!", Toast.LENGTH_SHORT).show();
                        pd.dismiss();
                    }
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
                    pd.dismiss();
                }
            });
        } else {
            Toast.makeText(getContext(), "No image selected", Toast.LENGTH_SHORT).show();
        }
    }
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
mvasco
  • 4,965
  • 7
  • 59
  • 120
  • 1
    If the linked duplicate doesn't work for your use-case, you will have to first download the image from the URL to the local device (either in memory or to a file on disk). Then you can upload that local image to Cloud Storage through the regular methods in the Firebase SDK as shown here: https://firebase.google.com/docs/storage/android/upload-files. – Frank van Puffelen Aug 29 '19 at 13:14
  • @FrankvanPuffelen, thank you. This was my first idea, to download the file locally and then upload it to Cloud Storage, but having the chance to use the URL directly, I only have to create a field in Cloud Storage with the URL and there is now no need to upload the file again to Cloud Storage – mvasco Aug 29 '19 at 14:55
  • 1
    OK, that's great to hear. In that case the marked duplicate works for your use-case. – Frank van Puffelen Aug 29 '19 at 15:23

0 Answers0