Android - Firebase - TaskSnapshot - Method should only be accessed within private scope?
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.
Related videos on Youtube
![Ryan](https://i.stack.imgur.com/Hr38U.jpg?s=256&g=1)
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, 2022Comments
-
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:
-
Ryan over 7 yearsI 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 over 7 yearsUsing 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 ispublic android.net.Uri getDownloadUrl()
. Do you see that also? -
dazza5000 over 7 yearsI'm having this same issue. Going to downgrade to 9.8.0 and see if I can get things working.
-
Ryan over 7 yearsI 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 over 7 yearsI'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 over 7 yearsI am getting the same behavior and was curious if anyone has reported it to Firebase. It doesn't seem to affect compiling.
-
JacksOnF1re over 7 yearsThe 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 over 7 yearsFor me this error appears with android studio 2.3, before the upgrade I was using 10.2.0 without problems.
-
-
IgorGanapolsky over 7 yearsFor some reason this annotation didn't make the warning go away for me.
-
Rapunzel Van Winkle over 7 yearsSorry 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 over 7 yearsIf 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 over 7 yearsI 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 over 7 yearsThank 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 almost 7 yearsIf @SuppressWarnings("VisibleForTests") Uri downloadUrl = taskSnapshot.getDownloadUrl(); does not work then, change your firebase version back to 9.8.0