How can I get the url of this link?
What you point at there is known as the download URL of the file. To get that URL, you'll want to follow the instructions on accessing a file through a URL:
StorageReference photoRef = storageRef.child("path/to/photo2.jpg");
photoRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
System.out.println(uri.toString());
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
throw exception
}
});
Related
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 ?
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:
the sample in the documentation on uploading a file
the documentation on downloading a file
How to use getdownloadurl in recent versions?
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
}
});
storageRef.child("users/me/profile.png").getBytes(Long.MAX_VALUE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
#Override
public void onSuccess(byte[] bytes) {
Uri image=uri;
Toast.makeText(viewpgdetails.this, "image uri="+image , Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
Toast.makeText(viewpgdetails.this, "failed to get image", Toast.LENGTH_SHORT).show();
// Handle any errors
}
});
// [END download_full_example]
}
It is unclear which type of URI you want, so I'll include both versions.
Google Storage URI
This is not very useful outside of a Google APIs.
This will be in the format: gs://PROJECT_ID.appspot.com/path/to/file.png
To get this value, you would use the following code:
String storageUri = storage.getReference('path/to/file.png').toString();
In the question's code, you would use:
StorageReference fileRef = storageRef.child("users/me/profile.png")
fileRef.getBytes(Long.MAX_VALUE)
.addOnSuccessListener(new OnSuccessListener<byte[]>() {
#Override
public void onSuccess(byte[] bytes) {
Toast.makeText(viewpgdetails.this, "image uri: " + fileRef.toString(), Toast.LENGTH_SHORT).show();
// Use the bytes to display the image
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
Toast.makeText(viewpgdetails.this, "failed to get image", Toast.LENGTH_SHORT).show();
// Handle any errors
}
});
HTTPS Resource URL
This URL would be used if you wanted to include the image in a web page or link to it in an email.
This will be in the format: https://firebasestorage.googleapis.com/v0/b/PROJECT_ID.appspot.com/o/path%2Fto%2Ffile.png?alt=media&token=ACCESS_TOKEN
Because this URL uses an access token issued by the server, it must be requested and returned to the client asynchronously. This is achieved with the following code:
Task<Uri> getDownloadUrlTask = storage.getReference('path/to/file.png').getDownloadUrl();
In the question's code, you would use:
StorageReference fileRef = storageRef.child("users/me/profile.png")
fileRef.getDownloadUrl()
.addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri downloadUrl) {
Toast.makeText(viewpgdetails.this, "image url: " + downloadUrl, Toast.LENGTH_SHORT).show();
// Use URL for internal web page, etc.
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
Toast.makeText(viewpgdetails.this, "failed to get image url", Toast.LENGTH_SHORT).show();
// Handle any errors
}
});
Note: If you want both the HTTPS URL and the file as a byte array, they would have to be requested as seperate Tasks using both getBytes() and getDownloadUrl().
See also
Cloud Storage for Android Guide
StorageReference API Reference
My code :
mStorageRef.child("image/pizza.jpg").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
// Got the download URL for 'users/me/profile.png'
System.out.println(uri.toString() + "!!!!!!");
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
// Handle any errors
}
});
But it only prints:
https://console.firebase.google.com/u/1/project/e-commerce-ddd1c/database/e-commerce-ddd1c/data
I don't understand how to get this url:
This is how I upload the image to Firebase storage.
Every time the app opens, I want to fetch image from storage and display in new activity.
StorageReference childRef=mStorage.child(newsnow).child("image3");
UploadTask uploadTask=childRef.putFile(uri3);
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(PostNews.this, "uploaded", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(PostNews.this, "no da", Toast.LENGTH_SHORT).show();
}
});
For retrieving an image from firebase you have to store image URL in your Firebase database. After retrieving an image URL you can use libraries like Picasso, glide and so many others for getting an image from URL. You can prefer this link for more description.
Something like this should to the trick:
ImageView image = (ImageView) findViewById(R.id.image_placeholder);
childRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
Picasso.with(MainActivity.this).load(uri).fit().centerCrop().into(image);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
// Handle any errors
}
});
Where image with R.id.image_placeholder is the place where you would like the image to be displayed.
Note that childRef is initialized in the same way that is now in your posted code.
I am trying to store the Firebase storage URL that I get once I upload a file to Firebase Storage, but for reasons I can't understand, I am unable to write to a location on my Firebase database inside of the Storage onSuccess listener. Any ideas why? If this is not the proper way to store the URL location of the uploaded file then what is?
I can't seem to find an example of this. This is for android.
Thank you
What you are trying to accomplish is completely doable, here's an example
StorageReference fileRef = ....;
Uri fileUri = ....;
UploadTask uploadTask = fileRef.putFile(fileUri);
uploadTask.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
// Handle unsuccessful uploads
Log.d("TAG", "onFailure: " + exception);
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// This is the URL you are looking for, here you should insert it into your db instance...Something like this
Uri downloadUrl = taskSnapshot.getDownloadUrl();
DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference();
dbRef.child("uploadUrls").child("url1").setValue(downloadUrl.toString());
}
});
I hope this helps
DatabaseReference dbRef = FirebaseDatabase.getInstance().getReference();
uploadTask.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
// Handle unsuccessful uploads
Log.d("TAG", "onFailure: " + exception);
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// This is the URL you are looking for, here you should insert it into your db instance...Something like this
Uri downloadUrl = taskSnapshot.getDownloadUrl();
addData(downloadUrl.toString());
}
});
public void addData(String downloadUrl)
{
dbRef.child("uploadUrls").child("url1").setValue(downloadUrl);
}