我正在将图片上传到应用中的 Firebase 存储。它上传良好,可以在 firebase storage online 中查看。当我尝试获取它并在 ImageView 中显示它时,它不会显示在 ImageView 中,而是 ImageView 也会消失。我已经用 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();
}
}
下载并加载到 ImageView 代码:
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);
}
});
最佳答案
用 glide 尝试一下..这段代码对我来说工作得很好
StorageReference storageReference = FirebaseStorage.getInstance().getReference().child("folderName"+"/"+thePicName);
Glide.with(context.getApplicationContext())
.using(new FirebaseImageLoader())
.load(storageReference)
.asBitmap()
.diskCacheStrategy(DiskCacheStrategy.NONE)
.priority(Priority.IMMEDIATE)
.into(new BitmapImageViewTarget(pic) {
@Override
protected void setResource(Bitmap resource) {
RoundedBitmapDrawable drawable = RoundedBitmapDrawableFactory.create(context.getResources(),
Bitmap.createScaledBitmap(resource, 150, 150, false));
drawable.setCircular(false);
pic.setImageDrawable(drawable);
}
});
关于java - 无法将图片从 Firebase 存储加载到 Imageview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59455429/