0

I have the following code snippet in my onSuccessListener() to save the download URL of the file I uploaded.

 DatabaseReference downloadURLRef = rootRef.child("Child").child(value);
 Uri downloadUrl = taskSnapshot.getDownloadUrl();
 downloadURLRef.setValue(downloadUrl);

I get this error:

Error TaskSnapshot - Method should only be accessed within private scope while using android studio 2.3.

When I followed this post and suppressed the warning, my app crashes on upload, and the upload fails. Here's the error for that.

What gives? This is straight from the Firebase documentation too.

EDIT: Upload code.

    storage = FirebaseStorage.getInstance();
    storageRef = storage.getReference(date + "/" + filename);
    uploadTask = storageRef.putFile(Uri.fromFile(file));

    // Register observers to listen for when the download is done or if it fails
    uploadTask.addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            // Handle unsuccessful uploads
        }
    }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

            // TODO: Fix this bullshit
            DatabaseReference downloadURLRef = rootRef.child("Child").child(value);
            Uri downloadUrl = taskSnapshot.getDownloadUrl();
            downloadURLRef.setValue(downloadUrl);
        }
    });
wasimsandhu
  • 4,296
  • 9
  • 27
  • 40

1 Answers1

1

Save downloadUrl as a string, not a Uri object:

    DatabaseReference downloadURLRef = rootRef.child("Child").child(value);
    Uri downloadUrl = taskSnapshot.getDownloadUrl();
    downloadURLRef.setValue(downloadUrl.toString()); // <= CHANGE
Bob Snyder
  • 37,759
  • 6
  • 111
  • 158
  • I also removed the @NonNull from the parameters in `onFailure`, for anyone who stumbles upon this response and it doesn't work. – wasimsandhu Jun 12 '17 at 06:29