Android - Firebase - TaskSnapshot - Method should only be accessed within private scope?

14,661

Solution 1

The problem seems to be caused by an overzealous Lint check. Try something like this:

@SuppressWarnings("VisibleForTests") Uri downloadUrl = taskSnapshot.getDownloadUrl();

This trick worked for me. If the problem's related to this bug report, then it should be fixed in 2.4.

Solution 2

I was stuck in the same issue and suppressWarnings didn't work for me. To get the complete download Uri i used the following code:

ref.putFile(imagePath).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override
                public void onSuccess(Uri uri) {
                     Log.d("URL", uri.toString());
                    // This is the complete uri, you can store it to realtime database
                }
            });
        }
    });

Hope this helps someone.

Share:
14,661

Related videos on Youtube

Ryan
Author by

Ryan

I love software development, computer science, mathematics, science, language, old-school video games, and about 10,000 other things. I learn a lot from Stack Overflow.

Updated on March 04, 2022

Comments

  • Ryan
    Ryan over 2 years

    Everything was working great... until I came back to work from a 3 month break and updated my Firebase from 9.8 to 10.0.1

    Now all of my calls to TaskSnapshot are giving me an error.

    Here is the example code that worked fine before:

    OnSuccessListener<UploadTask.TaskSnapshot> successListener = new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            attachments.add(fileName + "*-*" + taskSnapshot.getDownloadUrl().toString());
    
            numberOfCallbacks++;
            if (numberOfFiles == numberOfCallbacks) {
                currentUpload = false;
                onClickSendAlert(sendingView);
            }
        }
    };
    

    The error that I now get is regarding taskSnapshot.getDownloadUrl().

    Android Studio underlines that line in red and says:

    This method should only be accessed from tests or within private scope

    Can someone explain why this is happening? I have been researching all day for two days straight now and can't for the life of me figure this out (embarrassing).

    For what it's worth, this code is used to upload a file to Firebase Storage, then when it is complete (OnSuccess), it gets the download URL and stores it in the Firebase Database. This worked great before I updated to 10.0.1. I get the same error on my download tasks in another module.

    Here is a screenshot to give you a better visual of my situation:

    enter image description here

    • Ryan
      Ryan over 7 years
      I should add that as soon as I change Firebase and Google Play Service back to 9.8.0 all of the errors disappear.
    • Bob Snyder
      Bob Snyder over 7 years
      Using Android Studio, when I click on an occurrence of TaskSnapshot.getDownloadUrl() and hit Ctrl-B to see the decompiled class file, the declaration I see is public android.net.Uri getDownloadUrl(). Do you see that also?
    • dazza5000
      dazza5000 over 7 years
      I'm having this same issue. Going to downgrade to 9.8.0 and see if I can get things working.
    • Ryan
      Ryan over 7 years
      I ended up downgrading back to 9.8.0 as well and everything went back to normal. I hate that I don't have time to figure this out. Unless someone figures it out I will be stuck on 9.8.0. This project is almost complete anyways though so it shouldn't hurt much.
    • Travis Christian
      Travis Christian over 7 years
      I'm also getting this but I'm able to build, so I'm not sure why it's flagging as an error rather than a warning. Would like to know what correct method is.
    • GregM
      GregM over 7 years
      I am getting the same behavior and was curious if anyone has reported it to Firebase. It doesn't seem to affect compiling.
    • JacksOnF1re
      JacksOnF1re over 7 years
      The firebase guidelines even use this method for their examples: firebase.google.com/docs/storage/android/upload-files . Must be an oversight. Not the best but: disable inspection
    • Bronx
      Bronx over 7 years
      For me this error appears with android studio 2.3, before the upgrade I was using 10.2.0 without problems.
  • IgorGanapolsky
    IgorGanapolsky over 7 years
    For some reason this annotation didn't make the warning go away for me.
  • Rapunzel Van Winkle
    Rapunzel Van Winkle over 7 years
    Sorry to hear that! I'm using Android Studio 2.3 with Firebase 10.2.0 and I just double-checked that I can make the warning reappear when I remove the annotation and disappear when I put the annotation back.
  • Rapunzel Van Winkle
    Rapunzel Van Winkle over 7 years
    If you click on the red-underlined text (getDownloadUrl) and press alt-Enter, does the context menu say "Inspection 'Constant and Resource Type Mismatches' options"? Or does it mention some other inspection? If you click the right arrow on the menu option, it will give you more options (like "suppress for statement") to make the warning go away (which may or not be advisable, depending on what the warning's about).
  • IgorGanapolsky
    IgorGanapolsky over 7 years
    I do not get that context menu from alt-Enter. However, I do make the error go away by putting the annotation in the right place. I wasn't putting it on the right block of code.
  • Ryan
    Ryan over 7 years
    Thank you! I ended up taking such a long break from development that when I got back and upgraded to 10.2.0 the issue was gone :) I guess time can heal some things lol.
  • Aayush Upadhyay
    Aayush Upadhyay almost 7 years
    If @SuppressWarnings("VisibleForTests") Uri downloadUrl = taskSnapshot.getDownloadUrl(); does not work then, change your firebase version back to 9.8.0