I am uploading images from device storage to Firebase Storage using this:
final StorageReference fileReference = storageReference.child(System.currentTimeMillis()
+"."+getFileExtension(imageUri));
uploadTask = fileReference.putFile(imageUri);
But now I need to upload a picture from a URL. I have the picture URL and need to put that picture in Firebase Storage. I haven´t found any resources that explain this feature:
EDIT. Complete code for uploading files from device´s storage to Firebase Storage:
private void uploadImage(){
final ProgressDialog pd = new ProgressDialog(getContext());
pd.setMessage("Uploading");
pd.show();
if (imageUri != null){
final StorageReference fileReference = storageReference.child(System.currentTimeMillis()
+"."+getFileExtension(imageUri));
uploadTask = fileReference.putFile(imageUri);
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 fileReference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()){
Uri downloadUri = task.getResult();
String mUri = downloadUri.toString();
reference = FirebaseDatabase.getInstance().getReference("Users").child(fuser.getUid());
HashMap<String, Object> map = new HashMap<>();
map.put("imageURL", ""+mUri);
reference.updateChildren(map);
pd.dismiss();
} else {
Toast.makeText(getContext(), "Failed!", Toast.LENGTH_SHORT).show();
pd.dismiss();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
pd.dismiss();
}
});
} else {
Toast.makeText(getContext(), "No image selected", Toast.LENGTH_SHORT).show();
}
}