0

I have used this library in my Android app

implementation 'com.google.firebase:firebase-core:17.0.0' implementation 'com.google.firebase:firebase-firestore:20.0.0' implementation 'com.google.firebase:firebase-storage:18.0.0' 

And this method use to upload my image on Firebase Storage:

 StorageReference storageRef = mStorage.getReference(); finalStorageReference mountainsRef = storageRef.child("myImgName"); Uri file = Uri.fromFile(new File(myImgName)); UploadTask uploadTask = mountainsRef.putFile(file); uploadTask.addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception exception) { // Handle unsuccessful uploads progressDialog.dismiss(); Log.e(TAG, "img Error :" + exception.getMessage()); //Toast.makeText(MainActivity.this, "Failed "+e.getMessage(), Toast.LENGTH_SHORT).show(); } }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { @Override public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { progressDialog.dismiss(); //Log.e(TAG, "Task :" + taskSnapshot.getTask()); //Log.e(TAG, "Class Store:" + taskSnapshot.getStorage().getDownloadUrl()); Log.e(TAG,"metaData :"+taskSnapshot.getMetadata().getPath()); // taskSnapshot.getMetadata() contains file metadata such as size, content-type, etc. // ... } }).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() { @Override public void onProgress(UploadTask.TaskSnapshot taskSnapshot) { double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot .getTotalByteCount()); progressDialog.setMessage("Uploaded " + (int) progress + "%"); } }); 

this code perfectly working for me to upload image. but how to know the image location url ?

1
  • Note that Cloud Storage for Firebase (originally called Firebase Storage) is not the same as Cloud Firestore. Storage is a file-storage solution, while the latter is a NoSQL database. You're calling the Storage API, so please tag accordingly (as I did) Commented Jun 29, 2019 at 5:08

2 Answers 2

2

To get the download URL for a file in Cloud Storage, you call getDownloadUrl() on the StorageReference to that file. getDownloadUrl() returns a task, so you'll need to add a success listener to get the result.

mountainsRef.getStorage().getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() { @Override public void onSuccess(Uri uri) { // Got the download URL for 'users/me/profile.png' in uri System.out.println(uri.toString()); } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception exception) { // Handle any errors } }); 

For more on this, see:

Sign up to request clarification or add additional context in comments.

Comments

0

You can find your project's URL at the top of the Files section of Storage in the Firebase Console

 FirebaseStorage storage = FirebaseStorage.getInstance(); StorageReference storageRef = storage.getReferenceFromUrl("gs://example-firebase.appspot.com").child("android.jpg"); 

You can create a File object and attempt to load the file you want by calling getFile on your StorageReference with the new File object passed as a parameter. Since this operation happens asynchronously, you can add an OnSuccessListener and OnFailureListener

 try { final File localFile = File.createTempFile("images", "jpg"); storageRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() { @Override public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) { Bitmap bitmap = BitmapFactory.decodeFile(localFile.getAbsolutePath()); mImageView.setImageBitmap(bitmap); } }).addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception exception) { } }); } catch (IOException e ) {} 

You can get the file's Url by using the getDownloadUrl() method on your StorageReference, which will give you a Uri pointing to the file's location.

storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() { @Override public void onSuccess(Uri uri) { Log.e("Image +", "uri: " + uri.toString()); //Handle whatever you're going to do with the URL here } }); 

1 Comment

Use the section which you need. Thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.