I'm trying to upload and download images from Firabase Database which has an URL link to Firebase Storage. The problem is that the strange URL is being saved to database (see the link at the bottom). What should I do to obtain a normal URL that I will be able to use do downloand the image into my Android app? Thank you in advance!
Here I post some code I use:
Upload to Firebase DataBase and Storage:
mStorageRef = FirebaseStorage.getInstance().getReference();
mDataBaseRef = FirebaseDatabase.getInstance().getReference();
if (mImageUri != null)
{
final StorageReference fileReference = mStorageRef.child(nameimage + "." + getFileExtension(mImageUri));
fileReference.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(AddAdvertisement.this, "Upload successful!", Toast.LENGTH_LONG).show();
Upload upload = new Upload(et_localization, taskSnapshot.getUploadSessionUri().toString());
String uploadId = mDataBaseRef.push().getKey();
mDataBaseRef.child(uploadId).setValue(upload);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(AddAdvertisement.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
And download from Firebase:
databaseReference = FirebaseDatabase.getInstance().getReference();
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren())
{
Upload upload = postSnapshot.getValue(Upload.class);
mUploads.add(upload);
}
mAdapter = new AdverisementAdapter(getContext(),mUploads);
mrecyclerView.setAdapter(mAdapter);
}
and Picasso to retreive image:
@Override
public void onBindViewHolder(@NonNull ImageViewHolder imageViewHolder, int i) {
Upload uploadCurrent = mUploads.get(i);
imageViewHolder.textViewName.setText(uploadCurrent.getName());
Picasso.get().load(uploadCurrent.getUrl()).into(imageViewHolder.imageView);
}
Picasso work fine, beacuse except form an image I also get from Firebase string with the name, which is downloaded appropriately. So, the problem I think it's just with this wrong url:
When I try to entered this link, I receive this kind of error:
Invalid request. X-Goog-Upload-Command header is missing.
You're writing this value to the database:
taskSnapshot.getUploadSessionUri().toString()
This is the URI of the upload session, which you can use to resume an upload in case it gets aborted.
Since you want to store the download URL, this call is pretty useless for your cause. Instead you should call getDownloadUrl()
to (asynchronously) get the download URL for the newly uploaded file:
fileReference.putFile(mImageUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(AddAdvertisement.this, "Upload successful!", Toast.LENGTH_LONG).show();
fileReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
String url = uri.toString();
Upload upload = new Upload(et_localization, url);
String uploadId = mDataBaseRef.push().getKey();
mDataBaseRef.child(uploadId).setValue(upload);
}
});
}
})...
Note that this is quite well described in the Firebase documentation on getting a download URL after uploading a file, which event includes a sample of accomplishing the same by using continueWithTask
instead of nesting callbacks (which I did above).