如果我事先将URL存储为字符串,我可以在图像按钮中使用其URL下载图像,但是当我尝试使用存储引用将相同的图像加载到图像按钮中时,没有任何反应。我确实将存储引用转换为字符串,并将其显示在文本按钮上,以确保我拥有正确的引用。这里的变量l1、lp、ll1、lp1是LinearLayout和LayoutParams变量。
`Button[] dhababtnarr = new Button[t];
ll.addView(dhababtnarr[i], lp);
ImageButton imagebutton = new ImageButton(this);
strref = FirebaseStorage.getInstance().getReference();
StorageReference stt=strref.child("image1.jpg");
Glide.with(this)
.using(new FirebaseImageLoader())
.load(stt)
.into(imagebutton);
LinearLayout ll1 = (LinearLayout) findViewById(R.id.linear);
LayoutParams lp1 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ll1.addView(imagebutton, lp1);
dhababtnarr[1].setText(stt.toString());`此外,我还尝试使用.getDownloadUrl()下载URL并在glide中使用它,但.addOnSuccessListener和.addOnFailureListener永远不会执行。为了检查这一点,我在这些侦听器中设置了一个字符串为随机的"ss“值,并将我的按钮文本设置为该字符串,它不显示"ss”,而是为空。
`Button[] dhababtnarr = new Button[t];
ll.addView(dhababtnarr[i], lp);
ImageButton imagebutton = new ImageButton(this);
strref = FirebaseStorage.getInstance().getReference();
StorageReference stt=strref.child("image1.jpg");
String URI="";
stt.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
// Got the download URL for 'users/me/profile.png'
URI="s";
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
// Handle any errors
URI="s";
}
});
Glide.with(this)
.load(URI)
.into(imagebutton);
LinearLayout ll1 = (LinearLayout) findViewById(R.id.linear);
LayoutParams lp1 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ll1.addView(imagebutton, lp1);
dhababtnarr[1].setText(URI);`发布于 2018-04-16 14:57:21
如本post中所述,您可以创建一个位图,然后在imageview上设置它:
private Bitmap my_image;
StorageReference ref = storage.getReference().child("image1.jpg");
try {
final File localFile = File.createTempFile("Images", "bmp");
ref.getFile(localFile).addOnSuccessListener(new OnSuccessListener< FileDownloadTask.TaskSnapshot >() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
my_image = BitmapFactory.decodeFile(localFile.getAbsolutePath());
imagebutton.setImageBitmap(my_image)
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_LONG).show();
}
});
} catch (IOException e) {
e.printStackTrace();
}Glide的可以做到这一点:
在您的build.gradle中添加:
dependencies {
// FirebaseUI Storage only
compile 'com.firebaseui:firebase-ui-storage:0.6.0'
}然后在你的活动中试试这个:
// Reference to an image file in Firebase Storage
StorageReference storageReference = ...;
// ImageView in your Activity
ImageView imageView = ...;
// Load the image using Glide
Glide.with(this /* context */)
.using(new FirebaseImageLoader())
.load(storageReference)
.into(imageView);希望这能有所帮助!
https://stackoverflow.com/questions/49851103
复制相似问题