首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我需要帮助下载图像从firebase使用存储参考

我需要帮助下载图像从firebase使用存储参考
EN

Stack Overflow用户
提问于 2018-04-16 14:49:13
回答 1查看 633关注 0票数 0

如果我事先将URL存储为字符串,我可以在图像按钮中使用其URL下载图像,但是当我尝试使用存储引用将相同的图像加载到图像按钮中时,没有任何反应。我确实将存储引用转换为字符串,并将其显示在文本按钮上,以确保我拥有正确的引用。这里的变量l1、lp、ll1、lp1是LinearLayout和LayoutParams变量。

代码语言:javascript
复制
   `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”,而是为空。

代码语言:javascript
复制
   `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);`
EN

回答 1

Stack Overflow用户

发布于 2018-04-16 14:57:21

如本post中所述,您可以创建一个位图,然后在imageview上设置它:

代码语言:javascript
复制
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中添加:

代码语言:javascript
复制
dependencies {
    // FirebaseUI Storage only
    compile 'com.firebaseui:firebase-ui-storage:0.6.0'
}

然后在你的活动中试试这个:

代码语言:javascript
复制
// 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);

希望这能有所帮助!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49851103

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档