我正在学习一个android教程,并且我一直坚持使用被弃用的方法。我无法获取下载url以使我能够在ImageView中显示图像
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICTURE_RESULT && resultCode == RESULT_OK) {
Uri imageUri = data.getData();
StorageReference ref = FirebaseUtil.mStorageRef.child(imageUri.getLastPathSegment());
ref.putFile(imageUri).addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
String url = taskSnapshot.getDownloadUrl().toString();
String pictureName = taskSnapshot.getStorage().getPath();
deal.setImageUrl(url);
如何修改"String url = taskSnapshot.getDownloadUrl().toString();"
行才能获得下载链接?
发布于 2019-08-08 13:07:40
getDownloadUrl()
已弃用,因此您需要使用任务并检查isComplete()
,如下所示以查看是否已完成
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICTURE_RESULT && resultCode == RESULT_OK) {
Uri imageUri = data.getData();
StorageReference ref = FirebaseUtil.mStorageRef.child(imageUri.getLastPathSegment());
ref.putFile(imageUri).addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Task<Uri> uri = taskSnapshot.getStorage().getDownloadUrl();
while(!uri.isComplete());
Uri url = uri.getResult();
Log.i(TAG, url.toString());
deal.setImageUrl(url.toString());
发布于 2019-08-08 13:13:32
尝尝这个
taskSnapshot.getMetadata().getReference().getDownloadUrl().toString()
发布于 2019-08-08 13:44:01
尝尝这个
StorageReference imageRef = FirebaseUtil.mStorageRef.child(imageUri.getLastPathSegment());
UploadTask uploadTask = imageRef.putFile(imageUri);
uploadTask.continueWithTask(task -> {
if (!task.isSuccessful()) {
if (task.getException() != null)
throw task.getException();
}
return imageRef.getDownloadUrl();
}).addOnCompleteListener(task -> {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult()
} else {
if (task.getException() != null)
task.getException().printStackTrace();
}
});
https://stackoverflow.com/questions/57405414
复制相似问题