How to get URL from Firebase Storage getDownloadURL












12















I'm trying to get the "long term persistent download link" to files in our Firebase storage bucket.
I've changed the permissions of this to



service firebase.storage {
match /b/project-xxx.appspot.com/o {
match /{allPaths=**} {
allow read, write;
}
}
}


And my javacode looks like this:



private String niceLink (String date){
String link;
// Points to the root reference
StorageReference storageRef = FirebaseStorage.getInstance().getReference();
StorageReference dateRef = storageRef.child("/" + date+ ".csv");
link = dateRef.getDownloadUrl().toString();
return link;
}


When I run this I get the uri link that looks something like
com.google.android.gms.tasks.zzh@xxx



Question 1. Can I from this get the download link similar to: https://firebasestorage.googleapis.com/v0/b/project-xxxx.appspot.com/o/20-5-2016.csv?alt=media&token=b5d45a7f-3ab7-4f9b-b661-3a2187adxxxx



When trying to get the link above I changed the last row before my return, like this:



private String niceLink (String date){
String link;
// Points to the root reference
StorageReference storageRef = FirebaseStorage.getInstance().getReference();
StorageReference dateRef = storageRef.child("/" + date+ ".csv");
link = dateRef.getDownloadUrl().getResult().toString();
return link;
}


But when doing this i get a 403 error, and the app crashing. The consol tells me this is bc user is not logged in /auth.
"Please sign in before asking for token"



Question 2. How do I fix this?










share|improve this question





























    12















    I'm trying to get the "long term persistent download link" to files in our Firebase storage bucket.
    I've changed the permissions of this to



    service firebase.storage {
    match /b/project-xxx.appspot.com/o {
    match /{allPaths=**} {
    allow read, write;
    }
    }
    }


    And my javacode looks like this:



    private String niceLink (String date){
    String link;
    // Points to the root reference
    StorageReference storageRef = FirebaseStorage.getInstance().getReference();
    StorageReference dateRef = storageRef.child("/" + date+ ".csv");
    link = dateRef.getDownloadUrl().toString();
    return link;
    }


    When I run this I get the uri link that looks something like
    com.google.android.gms.tasks.zzh@xxx



    Question 1. Can I from this get the download link similar to: https://firebasestorage.googleapis.com/v0/b/project-xxxx.appspot.com/o/20-5-2016.csv?alt=media&token=b5d45a7f-3ab7-4f9b-b661-3a2187adxxxx



    When trying to get the link above I changed the last row before my return, like this:



    private String niceLink (String date){
    String link;
    // Points to the root reference
    StorageReference storageRef = FirebaseStorage.getInstance().getReference();
    StorageReference dateRef = storageRef.child("/" + date+ ".csv");
    link = dateRef.getDownloadUrl().getResult().toString();
    return link;
    }


    But when doing this i get a 403 error, and the app crashing. The consol tells me this is bc user is not logged in /auth.
    "Please sign in before asking for token"



    Question 2. How do I fix this?










    share|improve this question



























      12












      12








      12


      4






      I'm trying to get the "long term persistent download link" to files in our Firebase storage bucket.
      I've changed the permissions of this to



      service firebase.storage {
      match /b/project-xxx.appspot.com/o {
      match /{allPaths=**} {
      allow read, write;
      }
      }
      }


      And my javacode looks like this:



      private String niceLink (String date){
      String link;
      // Points to the root reference
      StorageReference storageRef = FirebaseStorage.getInstance().getReference();
      StorageReference dateRef = storageRef.child("/" + date+ ".csv");
      link = dateRef.getDownloadUrl().toString();
      return link;
      }


      When I run this I get the uri link that looks something like
      com.google.android.gms.tasks.zzh@xxx



      Question 1. Can I from this get the download link similar to: https://firebasestorage.googleapis.com/v0/b/project-xxxx.appspot.com/o/20-5-2016.csv?alt=media&token=b5d45a7f-3ab7-4f9b-b661-3a2187adxxxx



      When trying to get the link above I changed the last row before my return, like this:



      private String niceLink (String date){
      String link;
      // Points to the root reference
      StorageReference storageRef = FirebaseStorage.getInstance().getReference();
      StorageReference dateRef = storageRef.child("/" + date+ ".csv");
      link = dateRef.getDownloadUrl().getResult().toString();
      return link;
      }


      But when doing this i get a 403 error, and the app crashing. The consol tells me this is bc user is not logged in /auth.
      "Please sign in before asking for token"



      Question 2. How do I fix this?










      share|improve this question
















      I'm trying to get the "long term persistent download link" to files in our Firebase storage bucket.
      I've changed the permissions of this to



      service firebase.storage {
      match /b/project-xxx.appspot.com/o {
      match /{allPaths=**} {
      allow read, write;
      }
      }
      }


      And my javacode looks like this:



      private String niceLink (String date){
      String link;
      // Points to the root reference
      StorageReference storageRef = FirebaseStorage.getInstance().getReference();
      StorageReference dateRef = storageRef.child("/" + date+ ".csv");
      link = dateRef.getDownloadUrl().toString();
      return link;
      }


      When I run this I get the uri link that looks something like
      com.google.android.gms.tasks.zzh@xxx



      Question 1. Can I from this get the download link similar to: https://firebasestorage.googleapis.com/v0/b/project-xxxx.appspot.com/o/20-5-2016.csv?alt=media&token=b5d45a7f-3ab7-4f9b-b661-3a2187adxxxx



      When trying to get the link above I changed the last row before my return, like this:



      private String niceLink (String date){
      String link;
      // Points to the root reference
      StorageReference storageRef = FirebaseStorage.getInstance().getReference();
      StorageReference dateRef = storageRef.child("/" + date+ ".csv");
      link = dateRef.getDownloadUrl().getResult().toString();
      return link;
      }


      But when doing this i get a 403 error, and the app crashing. The consol tells me this is bc user is not logged in /auth.
      "Please sign in before asking for token"



      Question 2. How do I fix this?







      java android firebase firebase-storage






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jun 10 '18 at 13:42









      Doug Stevenson

      74.6k986106




      74.6k986106










      asked May 22 '16 at 13:16









      Jonathan FagerJonathan Fager

      63116




      63116
























          3 Answers
          3






          active

          oldest

          votes


















          13














          When you call getDownloadUrl(), the call is asynchronous and you must subscribe on a success callback to obtain the results:



          // Calls the server to securely obtain an unguessable download Url
          private void getUrlAsync (String date){
          // Points to the root reference
          StorageReference storageRef = FirebaseStorage.getInstance().getReference();
          StorageReference dateRef = storageRef.child("/" + date+ ".csv");
          dateRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>()
          {
          @Override
          public void onSuccess(Uri downloadUrl)
          {
          //do something with downloadurl
          }
          });
          }


          This will return a public unguessable download url. If you just uploaded a file, this public url will be in the success callback of the upload (you do not need to call another async method after you've uploaded).



          However, if all you want is a String representation of the reference, you can just call .toString()



          // Returns a Uri of the form gs://bucket/path that can be used
          // in future calls to getReferenceFromUrl to perform additional
          // actions
          private String niceRefLink (String date){
          // Points to the root reference
          StorageReference storageRef = FirebaseStorage.getInstance().getReference();
          StorageReference dateRef = storageRef.child("/" + date+ ".csv");
          return dateRef.toString();
          }





          share|improve this answer





















          • 1





            Using the Async i seem too be getting an error with the .addSuccessListener (Cannot resolve method). Furthermore, when I in the onSuccess method use something like link = downloadUrl.toString() it says it needs to be final. Any further assistance would be awesome!

            – Jonathan Fager
            May 24 '16 at 6:40








          • 1





            1. Sorry, that should have been addOnSuccessListener (typo) 2. when you get an error "needs to be final" in java it means there is a closure issue. See stackoverflow.com/questions/1299837/… fore more information. Basically, the short answer is, define the "link" variable as a class member and it should be fine.

            – Benjamin Wulfe
            May 24 '16 at 18:08






          • 1





            Worked like a sharm after some edits! Thx for the help!

            – Jonathan Fager
            May 24 '16 at 22:01



















          1














          //Firebase easy and Working uploads and downloads.



          @Override



          protected void onActivityResult(int requestCode, int resultCode, @Nullable final Intent data) {
          super.onActivityResult(requestCode, resultCode, data);
          if(requestCode == RC_SIGN_IN){
          if(resultCode == RESULT_OK){
          Toast.makeText(this,"Signed in!", LENGTH_SHORT).show();
          } else if(resultCode == RESULT_CANCELED){
          Toast.makeText(this,"Signed in canceled!", LENGTH_SHORT).show();
          finish();
          }
          } else if(requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK){

          // HERE I CALLED THAT METHOD
          uploadPhotoInFirebase(data);

          }
          }

          private void uploadPhotoInFirebase(@Nullable Intent data) {
          Uri selectedImageUri = data.getData();

          // Get a reference to store file at chat_photos/<FILENAME>
          final StorageReference photoRef = mChatPhotoStorageReference
          .child(selectedImageUri
          .getLastPathSegment());

          // Upload file to Firebase Storage
          photoRef.putFile(selectedImageUri)
          .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
          @Override
          public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

          // Download file From Firebase Storage
          photoRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
          @Override
          public void onSuccess(Uri downloadPhotoUrl) {
          //Now play with downloadPhotoUrl
          //Store data into Firebase Realtime Database
          FriendlyMessage friendlyMessage = new FriendlyMessage
          (null, mUsername, downloadPhotoUrl.toString());
          mDatabaseReference.push().setValue(friendlyMessage);
          }
          });
          }
          });
          }





          share|improve this answer

































            0














            StorageReference mStorageRef = FirebaseStorage.getInstance().getReference();

            final StorageReference fileupload=mStorageRef.child("Photos").child(fileUri.getLastPathSegment());
            UploadTask uploadTask = fileupload.putFile(fileUri);

            Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
            @Override
            public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
            if (!task.isSuccessful()) {
            throw task.getException();
            }
            return ref.getDownloadUrl();

            }
            }).addOnCompleteListener(new OnCompleteListener<Uri>() {
            @Override
            public void onComplete(@NonNull Task<Uri> task) {
            if (task.isSuccessful()) {
            Uri downloadUri = task.getResult();
            Picasso.get().load(downloadUri.toString()).into(image);

            } else {
            // Handle failures
            }
            }
            });





            share|improve this answer























              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              autoActivateHeartbeat: false,
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37374868%2fhow-to-get-url-from-firebase-storage-getdownloadurl%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              3 Answers
              3






              active

              oldest

              votes








              3 Answers
              3






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              13














              When you call getDownloadUrl(), the call is asynchronous and you must subscribe on a success callback to obtain the results:



              // Calls the server to securely obtain an unguessable download Url
              private void getUrlAsync (String date){
              // Points to the root reference
              StorageReference storageRef = FirebaseStorage.getInstance().getReference();
              StorageReference dateRef = storageRef.child("/" + date+ ".csv");
              dateRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>()
              {
              @Override
              public void onSuccess(Uri downloadUrl)
              {
              //do something with downloadurl
              }
              });
              }


              This will return a public unguessable download url. If you just uploaded a file, this public url will be in the success callback of the upload (you do not need to call another async method after you've uploaded).



              However, if all you want is a String representation of the reference, you can just call .toString()



              // Returns a Uri of the form gs://bucket/path that can be used
              // in future calls to getReferenceFromUrl to perform additional
              // actions
              private String niceRefLink (String date){
              // Points to the root reference
              StorageReference storageRef = FirebaseStorage.getInstance().getReference();
              StorageReference dateRef = storageRef.child("/" + date+ ".csv");
              return dateRef.toString();
              }





              share|improve this answer





















              • 1





                Using the Async i seem too be getting an error with the .addSuccessListener (Cannot resolve method). Furthermore, when I in the onSuccess method use something like link = downloadUrl.toString() it says it needs to be final. Any further assistance would be awesome!

                – Jonathan Fager
                May 24 '16 at 6:40








              • 1





                1. Sorry, that should have been addOnSuccessListener (typo) 2. when you get an error "needs to be final" in java it means there is a closure issue. See stackoverflow.com/questions/1299837/… fore more information. Basically, the short answer is, define the "link" variable as a class member and it should be fine.

                – Benjamin Wulfe
                May 24 '16 at 18:08






              • 1





                Worked like a sharm after some edits! Thx for the help!

                – Jonathan Fager
                May 24 '16 at 22:01
















              13














              When you call getDownloadUrl(), the call is asynchronous and you must subscribe on a success callback to obtain the results:



              // Calls the server to securely obtain an unguessable download Url
              private void getUrlAsync (String date){
              // Points to the root reference
              StorageReference storageRef = FirebaseStorage.getInstance().getReference();
              StorageReference dateRef = storageRef.child("/" + date+ ".csv");
              dateRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>()
              {
              @Override
              public void onSuccess(Uri downloadUrl)
              {
              //do something with downloadurl
              }
              });
              }


              This will return a public unguessable download url. If you just uploaded a file, this public url will be in the success callback of the upload (you do not need to call another async method after you've uploaded).



              However, if all you want is a String representation of the reference, you can just call .toString()



              // Returns a Uri of the form gs://bucket/path that can be used
              // in future calls to getReferenceFromUrl to perform additional
              // actions
              private String niceRefLink (String date){
              // Points to the root reference
              StorageReference storageRef = FirebaseStorage.getInstance().getReference();
              StorageReference dateRef = storageRef.child("/" + date+ ".csv");
              return dateRef.toString();
              }





              share|improve this answer





















              • 1





                Using the Async i seem too be getting an error with the .addSuccessListener (Cannot resolve method). Furthermore, when I in the onSuccess method use something like link = downloadUrl.toString() it says it needs to be final. Any further assistance would be awesome!

                – Jonathan Fager
                May 24 '16 at 6:40








              • 1





                1. Sorry, that should have been addOnSuccessListener (typo) 2. when you get an error "needs to be final" in java it means there is a closure issue. See stackoverflow.com/questions/1299837/… fore more information. Basically, the short answer is, define the "link" variable as a class member and it should be fine.

                – Benjamin Wulfe
                May 24 '16 at 18:08






              • 1





                Worked like a sharm after some edits! Thx for the help!

                – Jonathan Fager
                May 24 '16 at 22:01














              13












              13








              13







              When you call getDownloadUrl(), the call is asynchronous and you must subscribe on a success callback to obtain the results:



              // Calls the server to securely obtain an unguessable download Url
              private void getUrlAsync (String date){
              // Points to the root reference
              StorageReference storageRef = FirebaseStorage.getInstance().getReference();
              StorageReference dateRef = storageRef.child("/" + date+ ".csv");
              dateRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>()
              {
              @Override
              public void onSuccess(Uri downloadUrl)
              {
              //do something with downloadurl
              }
              });
              }


              This will return a public unguessable download url. If you just uploaded a file, this public url will be in the success callback of the upload (you do not need to call another async method after you've uploaded).



              However, if all you want is a String representation of the reference, you can just call .toString()



              // Returns a Uri of the form gs://bucket/path that can be used
              // in future calls to getReferenceFromUrl to perform additional
              // actions
              private String niceRefLink (String date){
              // Points to the root reference
              StorageReference storageRef = FirebaseStorage.getInstance().getReference();
              StorageReference dateRef = storageRef.child("/" + date+ ".csv");
              return dateRef.toString();
              }





              share|improve this answer















              When you call getDownloadUrl(), the call is asynchronous and you must subscribe on a success callback to obtain the results:



              // Calls the server to securely obtain an unguessable download Url
              private void getUrlAsync (String date){
              // Points to the root reference
              StorageReference storageRef = FirebaseStorage.getInstance().getReference();
              StorageReference dateRef = storageRef.child("/" + date+ ".csv");
              dateRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>()
              {
              @Override
              public void onSuccess(Uri downloadUrl)
              {
              //do something with downloadurl
              }
              });
              }


              This will return a public unguessable download url. If you just uploaded a file, this public url will be in the success callback of the upload (you do not need to call another async method after you've uploaded).



              However, if all you want is a String representation of the reference, you can just call .toString()



              // Returns a Uri of the form gs://bucket/path that can be used
              // in future calls to getReferenceFromUrl to perform additional
              // actions
              private String niceRefLink (String date){
              // Points to the root reference
              StorageReference storageRef = FirebaseStorage.getInstance().getReference();
              StorageReference dateRef = storageRef.child("/" + date+ ".csv");
              return dateRef.toString();
              }






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Apr 1 '18 at 18:10









              CopsOnRoad

              3,64921420




              3,64921420










              answered May 22 '16 at 20:21









              Benjamin WulfeBenjamin Wulfe

              1,07746




              1,07746








              • 1





                Using the Async i seem too be getting an error with the .addSuccessListener (Cannot resolve method). Furthermore, when I in the onSuccess method use something like link = downloadUrl.toString() it says it needs to be final. Any further assistance would be awesome!

                – Jonathan Fager
                May 24 '16 at 6:40








              • 1





                1. Sorry, that should have been addOnSuccessListener (typo) 2. when you get an error "needs to be final" in java it means there is a closure issue. See stackoverflow.com/questions/1299837/… fore more information. Basically, the short answer is, define the "link" variable as a class member and it should be fine.

                – Benjamin Wulfe
                May 24 '16 at 18:08






              • 1





                Worked like a sharm after some edits! Thx for the help!

                – Jonathan Fager
                May 24 '16 at 22:01














              • 1





                Using the Async i seem too be getting an error with the .addSuccessListener (Cannot resolve method). Furthermore, when I in the onSuccess method use something like link = downloadUrl.toString() it says it needs to be final. Any further assistance would be awesome!

                – Jonathan Fager
                May 24 '16 at 6:40








              • 1





                1. Sorry, that should have been addOnSuccessListener (typo) 2. when you get an error "needs to be final" in java it means there is a closure issue. See stackoverflow.com/questions/1299837/… fore more information. Basically, the short answer is, define the "link" variable as a class member and it should be fine.

                – Benjamin Wulfe
                May 24 '16 at 18:08






              • 1





                Worked like a sharm after some edits! Thx for the help!

                – Jonathan Fager
                May 24 '16 at 22:01








              1




              1





              Using the Async i seem too be getting an error with the .addSuccessListener (Cannot resolve method). Furthermore, when I in the onSuccess method use something like link = downloadUrl.toString() it says it needs to be final. Any further assistance would be awesome!

              – Jonathan Fager
              May 24 '16 at 6:40







              Using the Async i seem too be getting an error with the .addSuccessListener (Cannot resolve method). Furthermore, when I in the onSuccess method use something like link = downloadUrl.toString() it says it needs to be final. Any further assistance would be awesome!

              – Jonathan Fager
              May 24 '16 at 6:40






              1




              1





              1. Sorry, that should have been addOnSuccessListener (typo) 2. when you get an error "needs to be final" in java it means there is a closure issue. See stackoverflow.com/questions/1299837/… fore more information. Basically, the short answer is, define the "link" variable as a class member and it should be fine.

              – Benjamin Wulfe
              May 24 '16 at 18:08





              1. Sorry, that should have been addOnSuccessListener (typo) 2. when you get an error "needs to be final" in java it means there is a closure issue. See stackoverflow.com/questions/1299837/… fore more information. Basically, the short answer is, define the "link" variable as a class member and it should be fine.

              – Benjamin Wulfe
              May 24 '16 at 18:08




              1




              1





              Worked like a sharm after some edits! Thx for the help!

              – Jonathan Fager
              May 24 '16 at 22:01





              Worked like a sharm after some edits! Thx for the help!

              – Jonathan Fager
              May 24 '16 at 22:01













              1














              //Firebase easy and Working uploads and downloads.



              @Override



              protected void onActivityResult(int requestCode, int resultCode, @Nullable final Intent data) {
              super.onActivityResult(requestCode, resultCode, data);
              if(requestCode == RC_SIGN_IN){
              if(resultCode == RESULT_OK){
              Toast.makeText(this,"Signed in!", LENGTH_SHORT).show();
              } else if(resultCode == RESULT_CANCELED){
              Toast.makeText(this,"Signed in canceled!", LENGTH_SHORT).show();
              finish();
              }
              } else if(requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK){

              // HERE I CALLED THAT METHOD
              uploadPhotoInFirebase(data);

              }
              }

              private void uploadPhotoInFirebase(@Nullable Intent data) {
              Uri selectedImageUri = data.getData();

              // Get a reference to store file at chat_photos/<FILENAME>
              final StorageReference photoRef = mChatPhotoStorageReference
              .child(selectedImageUri
              .getLastPathSegment());

              // Upload file to Firebase Storage
              photoRef.putFile(selectedImageUri)
              .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
              @Override
              public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

              // Download file From Firebase Storage
              photoRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
              @Override
              public void onSuccess(Uri downloadPhotoUrl) {
              //Now play with downloadPhotoUrl
              //Store data into Firebase Realtime Database
              FriendlyMessage friendlyMessage = new FriendlyMessage
              (null, mUsername, downloadPhotoUrl.toString());
              mDatabaseReference.push().setValue(friendlyMessage);
              }
              });
              }
              });
              }





              share|improve this answer






























                1














                //Firebase easy and Working uploads and downloads.



                @Override



                protected void onActivityResult(int requestCode, int resultCode, @Nullable final Intent data) {
                super.onActivityResult(requestCode, resultCode, data);
                if(requestCode == RC_SIGN_IN){
                if(resultCode == RESULT_OK){
                Toast.makeText(this,"Signed in!", LENGTH_SHORT).show();
                } else if(resultCode == RESULT_CANCELED){
                Toast.makeText(this,"Signed in canceled!", LENGTH_SHORT).show();
                finish();
                }
                } else if(requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK){

                // HERE I CALLED THAT METHOD
                uploadPhotoInFirebase(data);

                }
                }

                private void uploadPhotoInFirebase(@Nullable Intent data) {
                Uri selectedImageUri = data.getData();

                // Get a reference to store file at chat_photos/<FILENAME>
                final StorageReference photoRef = mChatPhotoStorageReference
                .child(selectedImageUri
                .getLastPathSegment());

                // Upload file to Firebase Storage
                photoRef.putFile(selectedImageUri)
                .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                @Override
                public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                // Download file From Firebase Storage
                photoRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                @Override
                public void onSuccess(Uri downloadPhotoUrl) {
                //Now play with downloadPhotoUrl
                //Store data into Firebase Realtime Database
                FriendlyMessage friendlyMessage = new FriendlyMessage
                (null, mUsername, downloadPhotoUrl.toString());
                mDatabaseReference.push().setValue(friendlyMessage);
                }
                });
                }
                });
                }





                share|improve this answer




























                  1












                  1








                  1







                  //Firebase easy and Working uploads and downloads.



                  @Override



                  protected void onActivityResult(int requestCode, int resultCode, @Nullable final Intent data) {
                  super.onActivityResult(requestCode, resultCode, data);
                  if(requestCode == RC_SIGN_IN){
                  if(resultCode == RESULT_OK){
                  Toast.makeText(this,"Signed in!", LENGTH_SHORT).show();
                  } else if(resultCode == RESULT_CANCELED){
                  Toast.makeText(this,"Signed in canceled!", LENGTH_SHORT).show();
                  finish();
                  }
                  } else if(requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK){

                  // HERE I CALLED THAT METHOD
                  uploadPhotoInFirebase(data);

                  }
                  }

                  private void uploadPhotoInFirebase(@Nullable Intent data) {
                  Uri selectedImageUri = data.getData();

                  // Get a reference to store file at chat_photos/<FILENAME>
                  final StorageReference photoRef = mChatPhotoStorageReference
                  .child(selectedImageUri
                  .getLastPathSegment());

                  // Upload file to Firebase Storage
                  photoRef.putFile(selectedImageUri)
                  .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                  @Override
                  public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                  // Download file From Firebase Storage
                  photoRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                  @Override
                  public void onSuccess(Uri downloadPhotoUrl) {
                  //Now play with downloadPhotoUrl
                  //Store data into Firebase Realtime Database
                  FriendlyMessage friendlyMessage = new FriendlyMessage
                  (null, mUsername, downloadPhotoUrl.toString());
                  mDatabaseReference.push().setValue(friendlyMessage);
                  }
                  });
                  }
                  });
                  }





                  share|improve this answer















                  //Firebase easy and Working uploads and downloads.



                  @Override



                  protected void onActivityResult(int requestCode, int resultCode, @Nullable final Intent data) {
                  super.onActivityResult(requestCode, resultCode, data);
                  if(requestCode == RC_SIGN_IN){
                  if(resultCode == RESULT_OK){
                  Toast.makeText(this,"Signed in!", LENGTH_SHORT).show();
                  } else if(resultCode == RESULT_CANCELED){
                  Toast.makeText(this,"Signed in canceled!", LENGTH_SHORT).show();
                  finish();
                  }
                  } else if(requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK){

                  // HERE I CALLED THAT METHOD
                  uploadPhotoInFirebase(data);

                  }
                  }

                  private void uploadPhotoInFirebase(@Nullable Intent data) {
                  Uri selectedImageUri = data.getData();

                  // Get a reference to store file at chat_photos/<FILENAME>
                  final StorageReference photoRef = mChatPhotoStorageReference
                  .child(selectedImageUri
                  .getLastPathSegment());

                  // Upload file to Firebase Storage
                  photoRef.putFile(selectedImageUri)
                  .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                  @Override
                  public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                  // Download file From Firebase Storage
                  photoRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                  @Override
                  public void onSuccess(Uri downloadPhotoUrl) {
                  //Now play with downloadPhotoUrl
                  //Store data into Firebase Realtime Database
                  FriendlyMessage friendlyMessage = new FriendlyMessage
                  (null, mUsername, downloadPhotoUrl.toString());
                  mDatabaseReference.push().setValue(friendlyMessage);
                  }
                  });
                  }
                  });
                  }






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 8 '18 at 0:19

























                  answered Nov 8 '18 at 0:12









                  Subhojit HalderSubhojit Halder

                  113




                  113























                      0














                      StorageReference mStorageRef = FirebaseStorage.getInstance().getReference();

                      final StorageReference fileupload=mStorageRef.child("Photos").child(fileUri.getLastPathSegment());
                      UploadTask uploadTask = fileupload.putFile(fileUri);

                      Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
                      @Override
                      public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                      if (!task.isSuccessful()) {
                      throw task.getException();
                      }
                      return ref.getDownloadUrl();

                      }
                      }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                      @Override
                      public void onComplete(@NonNull Task<Uri> task) {
                      if (task.isSuccessful()) {
                      Uri downloadUri = task.getResult();
                      Picasso.get().load(downloadUri.toString()).into(image);

                      } else {
                      // Handle failures
                      }
                      }
                      });





                      share|improve this answer




























                        0














                        StorageReference mStorageRef = FirebaseStorage.getInstance().getReference();

                        final StorageReference fileupload=mStorageRef.child("Photos").child(fileUri.getLastPathSegment());
                        UploadTask uploadTask = fileupload.putFile(fileUri);

                        Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
                        @Override
                        public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                        if (!task.isSuccessful()) {
                        throw task.getException();
                        }
                        return ref.getDownloadUrl();

                        }
                        }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                        @Override
                        public void onComplete(@NonNull Task<Uri> task) {
                        if (task.isSuccessful()) {
                        Uri downloadUri = task.getResult();
                        Picasso.get().load(downloadUri.toString()).into(image);

                        } else {
                        // Handle failures
                        }
                        }
                        });





                        share|improve this answer


























                          0












                          0








                          0







                          StorageReference mStorageRef = FirebaseStorage.getInstance().getReference();

                          final StorageReference fileupload=mStorageRef.child("Photos").child(fileUri.getLastPathSegment());
                          UploadTask uploadTask = fileupload.putFile(fileUri);

                          Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
                          @Override
                          public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                          if (!task.isSuccessful()) {
                          throw task.getException();
                          }
                          return ref.getDownloadUrl();

                          }
                          }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                          @Override
                          public void onComplete(@NonNull Task<Uri> task) {
                          if (task.isSuccessful()) {
                          Uri downloadUri = task.getResult();
                          Picasso.get().load(downloadUri.toString()).into(image);

                          } else {
                          // Handle failures
                          }
                          }
                          });





                          share|improve this answer













                          StorageReference mStorageRef = FirebaseStorage.getInstance().getReference();

                          final StorageReference fileupload=mStorageRef.child("Photos").child(fileUri.getLastPathSegment());
                          UploadTask uploadTask = fileupload.putFile(fileUri);

                          Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
                          @Override
                          public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
                          if (!task.isSuccessful()) {
                          throw task.getException();
                          }
                          return ref.getDownloadUrl();

                          }
                          }).addOnCompleteListener(new OnCompleteListener<Uri>() {
                          @Override
                          public void onComplete(@NonNull Task<Uri> task) {
                          if (task.isSuccessful()) {
                          Uri downloadUri = task.getResult();
                          Picasso.get().load(downloadUri.toString()).into(image);

                          } else {
                          // Handle failures
                          }
                          }
                          });






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Sep 14 '18 at 14:30









                          RajRaj

                          2,4392519




                          2,4392519






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37374868%2fhow-to-get-url-from-firebase-storage-getdownloadurl%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Monofisismo

                              Angular Downloading a file using contenturl with Basic Authentication

                              Olmecas