1

I am uploading an image from my local storage to firebase, although with my code I would like to get that uploaded image URL and store it into a string. How can I do this?

        StorageReference filepath1 = mStorage.child("Blog_Image").child(mcontentURI.getLastPathSegment() + random());

        final String c;
        filepath1.putFile(mcontentURI).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {

            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                final Uri downlaodUrl = taskSnapshot.getDownloadUrl();
                String b = downlaodUrl.toString();
                c = b;

            }

        });
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Your code seems to be doing just that. What's the problem? – Frank van Puffelen Sep 13 '17 at 14:14
  • its not. since i cant assign it to a final variable – rusty molle Sep 13 '17 at 14:16
  • Ah, got it. You want to export a value out of an inner class. That will require some magic with a fixed length array, as shown in [this answer](https://stackoverflow.com/questions/5977735/setting-outer-variable-from-anonymous-inner-class). But note that your variable `c` will only get its value once `onSuccess()` runs. So typically all code that requires access to the download URL should be **inside** the `onSuccess` method. – Frank van Puffelen Sep 13 '17 at 14:44
  • is there any other way I can get the image URL? – rusty molle Sep 13 '17 at 16:13
  • basically, I have 1 string, containing text and a number of X local images. and I would like to replace those local images with its uploaded firebase URL. So i would need to store those uploaded image URL's into a number of Strings. Then replace the local images "Content://...." with its corrosponding firebase URL. – rusty molle Sep 13 '17 at 16:20

1 Answers1

0

Try the below change. It should work:

StorageReference filepath1 = mStorage.child("Blog_Image").child(mcontentURI.getLastPathSegment() + random());
    public static String c;
    filepath1.putFile(mcontentURI).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
            String b = taskSnapshot.getDownloadUrl().toString();
            c = b;
        }
    });
Abhishek
  • 1,261
  • 1
  • 13
  • 30
  • It wont work as, I can't assign c to b since its a final variable – rusty molle Sep 13 '17 at 14:20
  • @rustymolle - sorry, I thought I had fixed that in my first reply. But now I have. Change final to Static and it will work. – Abhishek Sep 13 '17 at 15:00
  • remains null :( – rusty molle Sep 13 '17 at 16:09
  • @rustymolle - can you check separately, if 'b' is also null. Or just 'c'. If 'b' is fine, then all you need to do is declare 'public static String C' at the beginning of the Class. And it should work – Abhishek Sep 13 '17 at 16:14
  • But that cant be correct since it outputs an image just fine. – rusty molle Sep 13 '17 at 18:40
  • so is 'b' null or not? – Abhishek Sep 14 '17 at 07:00
  • it cant be since, im able to use b within the onSuccess method. Like @Frank van Puffelen said - "c will only get its value once onSuccess() runs. So typically all code that requires access to the download URL should be inside the onSuccess method." – rusty molle Sep 14 '17 at 09:21
  • You could take a look for yourself and download my repo: https://github.com/rasselll/MessagingApp-master-master1 if you do, the file is PostActivity.java. Here I have stored b as newimg and stored that in child("content").... You can see it posts as null. if you do run the app, you'll need to title and decription feilds in also click the underline button to select an image. then "Publish". – rusty molle Sep 14 '17 at 10:59
  • I read your java file. The first problem that I detected is that the file that you are trying to store on Firebase Storage is supposed to be have URI - mContentURI. final String countImg = Integer.toString(count) AND mcontentURI = Uri.parse(countImg); So countImg is a number in String format and not a URI and hence the OnSuccessListener will not be triggered when you try and save the file. – Abhishek Sep 14 '17 at 15:09