我正在使用以下方法将图像从设备存储上传到Firebase存储:
final StorageReference fileReference = storageReference.child(System.currentTimeMillis()
+"."+getFileExtension(imageUri));
uploadTask = fileReference.putFile(imageUri);
但是现在我需要从URL上传图片。 我有图片URL,需要将图片放入Firebase存储中。 我没有找到任何可以解释此功能的资源:
编辑。用于将文件从设备存储上传到Firebase存储的完整代码:
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();
}
}