【发布时间】:2019-12-25 20:44:13
【问题描述】:
我正在我的应用中将图片上传到 Firebase 存储。它可以很好地上传,并且可以在 firebase 存储中在线查看。当我试图获取它并在图像视图中显示它时,它不会显示在图像视图中,而是图像视图也会消失。我已经用 Piccaso 和 glide 尝试过这个,但没有任何效果。下面附上代码
上传代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE && resultCode == RESULT_OK && data != null && data.getData() != null) {
uri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
profile_image = findViewById(R.id.profileimage);
profile_image.setImageBitmap(bitmap);
imageRef=storageReference.child(userID);
imageRef.putFile(uri)
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
//if the upload is successful
//hiding the progress dialog
//and displaying a success toast
//dismissDialog();
profilePicUrl = taskSnapshot.getMetadata().getReference().getDownloadUrl().toString();
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
//if the upload is not successful
//hiding the progress dialog
// dismissDialog();
//and displaying error message
Toast.makeText(profile.this, exception.getCause().getLocalizedMessage(), Toast.LENGTH_LONG).show();
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
下载并加载到图像视图代码:
fbstorage=FirebaseStorage.getInstance();
storageReference=fbstorage.getReference();
imageRef=storageReference.child(userID);
final String h=imageRef.getDownloadUrl().toString();
storageReference.child(userID).getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
Toast.makeText(profile.this, "hello", Toast.LENGTH_SHORT).show();
Picasso.with(profile.this).load(h).into(profile_image);
}
});
【问题讨论】:
-
上传文件时是否尝试将图像设置为
onSuccess()内部的视图? -
@AlexMamo 是的,同样的问题
-
你为什么加载
h而不是你刚刚检索到的uri?
标签: java android firebase firebase-storage