1

There's a mistake in this code:

String download_url=task.getResult.getStorage.getDownloadUrl.toString);

When I run the program, I choose a picture from the gallery and I post it, and I get a message:

User is not authenticated, please authenticate using Firebase Authentication and try again

final StorageReference newPhoto=mPhotosStrorage.child(imageUri.getLastPathSegment());
newPhoto.putFile(imageUri).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {

        if (task.isSuccessful())
        {
            final String myKey=mPhotosDatabase.push().getKey();

 //this error       String download_url=task.getResult().getDownloadUrl().toString();

            String datem=getDateTime();              

            DatabaseReference newDatabase=mPhotosDatabase.child(myKey);

            newDatabase.child("postid").setValue(myKey);
            newDatabase.child("postedby").setValue(userId);
            newDatabase.child("postedon").setValue(datem);
            newDatabase.child("postdetails").setValue(post);
            newDatabase.child("postlikes");
            newDatabase.child("postviews");
            newDatabase.child("postcomments");

            newDatabase.child("postimage").setValue(download_url).addOnCompleteListener(new OnCompleteListener<Void>() {
                @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
                @Override
                public void onComplete(@NonNull Task<Void> task) {

                    if (task.isSuccessful())
                    {
                        pb.setVisibility(View.GONE);
                        Pair[] pairs=new Pair[1];
                        pairs[0]=new Pair<View,String>(homeLayout,"etTransition");

                        ActivityOptions options=ActivityOptions.makeSceneTransitionAnimation(PostActivity.this,pairs);

                        startActivity(new Intent(PostActivity.this,HomeActivity.class),options.toBundle());

                    }
                }
            });
        }else {
            Toast.makeText(PostActivity.this, "Error:"+task.getException().getMessage(), Toast.LENGTH_SHORT).show();
        }
    }
});

Please help solve it and rewrite the code for me.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • What exactly in this code doesn't work the way you expect? Tell us what is wrong with shared code. Do you have any errors? – Alex Mamo Sep 29 '21 at 11:36
  • @AlexMamo String download_url=task.getResult().getDownloadUrl().toString(); –  Sep 29 '21 at 11:37
  • @AlexMamo getDownloadUrl() is error –  Sep 29 '21 at 11:38
  • The error is quite explicit: "User is not authenticated, please authenticate using Firebase Authentication and try again" So you'll have to sing with Firebase Authentication before calling `getDownloadURL()`. – Frank van Puffelen Sep 29 '21 at 12:19
  • Determining the download URL requires a call to the server. Because of this, the call to `getDownloadUrl()` returns a `Task` that completes when the download URL comes back from the server. You'll need to call `addSuccessListener()` on it to wait for it to complete. See the documentation [here](https://firebase.google.com/docs/storage/android/download-files#download_data_via_url) and this [answer](https://stackoverflow.com/a/51064689) – Frank van Puffelen Sep 29 '21 at 12:20

1 Answers1

0

According to your last comment, the problem is at the following line of code:

String download_url=task.getResult().getDownloadUrl().toString();

Please note that this is not how you get the download URL nowadays. Converting that Task object to String isn't helpful at all. To solve this, please use the following lines of code:

storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
    @Override
    public void onSuccess(Uri uri) {
        String url = uri.toString();
        //Do what you need to do with the URL
    }
});

Besides that, always make sure to have the user authenticated, as that error occurs only when the user is not authenticated and the rules are set to reject the operations.

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193