0

I'm a new member. I'm also relatively new to java and, in the last few weeks, firebase. I'm trying to stumble my way through the codelabs frebase android tutorial and have got as far as the very last updated required in "part 8" of the lesson at https://codelabs.developers.google.com/codelabs/firebase-android/#7

At the end of part 8 we are requested to add a "putImageInStorage" method into MainActivity.java

private void putImageInStorage(StorageReference storageReference, Uri uri, final String key) {storageReference.putFile(uri).addOnCompleteListener(MainActivity.this,
       new OnCompleteListener<UploadTask.TaskSnapshot>() {
           @Override
           public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
               if (task.isSuccessful()) {
                   FriendlyMessage friendlyMessage =
                           new FriendlyMessage(null, mUsername, mPhotoUrl,
                                   task.getResult().getMetadata().**getDownloadUrl()**
                                           .toString());
                   mFirebaseDatabaseReference.child(MESSAGES_CHILD).child(key)
                           .setValue(friendlyMessage);
               } else {
                   Log.w(TAG, "Image upload task was not successful.",
                           task.getException());
               }
           }
       });
}    

However, when i ran this as instructed the IDE would not compile, citing the following section as being the problem: ".getDownloadUrl()" I have spent several hours looking around for a solution, and it appears this has been "deprecated". I did try to swap out the line of code as suggested at getDownloadUrl() of firebase storage return the same and wrong link for all the uploaded files. How to fix this? (ie use * task.getMetadata().getReference().getDownloadUrl().toString() * instead of - * task.getResult().getStorage().getDownloadUrl().toString(), but this did not appear to work. I then tried running this method i'd found on stackoverflow, but it now crashes my app("Unfortunately, Friendly Chat has stopped")

private void putImageInStorage(final StorageReference storageReference, Uri 
uri, final String key) {
    storageReference.putFile(uri).addOnCompleteListener(
            new OnCompleteListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
                    if (task.isSuccessful()) {
                        FriendlyMessage friendlyMessage =
                                new FriendlyMessage(null, mUsername, mPhotoUrl,
                                        task.getResult().getStorage().getDownloadUrl().toString() , mFirebaseUser.getUid());
                        mFirebaseDatabaseReference.child(MESSAGES_CHILD).child(key)
                                .setValue(friendlyMessage);
                    } else {
                        Log.w(TAG, "Image upload task was not successful.",
                                task.getException());
                    }
                }
            });
}

Any solutions, especially explained for an absolute novice, would be very gratefully received. Thank you for your time to read this, and any help.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Rumpole
  • 1
  • 1
  • You will need to add a completion listener on the `Task` that is returned from `StorageReference.getDownloadUrl()`. See the first example in this answer: https://stackoverflow.com/a/37379251/209103 – Frank van Puffelen Jul 03 '18 at 16:25
  • Thank you Frank, but I'm simply not at a level to understand that answer yet. I used "Ankode's" answer at https://github.com/firebase/friendlychat-android/issues/51 GradleBuild does now build the app now without the previous error, but it wont load on my tablet (pop up "Unfortunately, Friendly Chat has stopped") – Rumpole Jul 04 '18 at 11:38
  • That means that an exception occurred. Run the app from Android Studio with a debugger attached, and the exception will be shown in your logcat output. – Frank van Puffelen Jul 04 '18 at 14:02
  • Frank, thanks for prompt reply. I tried "Attach debugger to Android Process" in Android Studio, and (eventually was able to) select "com.google.firebase.codelab.friendlychat" (it would not allow me to select my device). Ultimately I got a lot of lines in my "View>Tools>Logcat", but this is unintelligible to me. – Rumpole Jul 04 '18 at 15:24
  • When i selected "error" on logcat I have about 2-3 pages of code I don't understand. Is there anyway to post this on here, or is that inappropriate? – Rumpole Jul 04 '18 at 16:51
  • Goedemorgen Frank, I've tried to use the methods at github.com/firebase/friendlychat-android/issues/51 but I couldn't get either to work, largely because I'm probably not at the required level of programming . Is there anyway the Codelab could be updated so it just works? After all its supposed to be the introduction to the platform and, i wish it were not so, but I have found it very difficult to get working for many of the steps. Or is there anyway you could please look at my code? Dankuwel. Tot ziens. – Rumpole Jul 05 '18 at 16:01
  • I already filed a bug to get the codelab updated. If that's what you're after, I recommend [filing a bug report](https://firebase.google.com/support/contact/bugs-features/). But the change to the codelab will basically be the same as in the answer I linked, which is why I marked your question as a duplicate. If you do the same as `getUrlAsync` in the linked answer does, you'll get the actual download URL. – Frank van Puffelen Jul 05 '18 at 16:32

0 Answers0