【问题标题】:I need help to download image from firebase using storage reference我需要帮助才能使用存储参考从 Firebase 下载图像
【发布时间】:2018-04-19 23:27:40
【问题描述】:

如果我事先将 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);`

【问题讨论】:

    标签: android google-cloud-firestore android-glide


    【解决方案1】:

    正如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);
    

    希望这会有所帮助!

    【讨论】:

    • 我尝试了该代码,但收到“发生未知错误,请检查 HTTP 结果代码和服务器响应的内部异常”。作为我的活动的祝酒词,没有图像加载。如果您能帮助我使用 glide 来代替位图下载图像,那将是一个巨大的帮助
    • 我已经添加了滑翔的解决方案。
    • 很抱歉最近几天没有回复。我在哪里准确添加此代码,当我将其添加到我的 java 类时,它在最后一行中显示“无法解析符号 'Factory'”。以及@override 处显示的“方法不会从其超类覆盖”消息。非常感谢您的帮助。
    • 嘿,检查一下github.com/firebase/FirebaseUI-Android/tree/master/storage,尝试按照自述文件中的说明进行操作,如果您不能按照说明进行操作,请分享,我将为您创建一个工作示例
    • 添加依赖:实现 'com.github.bumptech.glide:glide:4.7.1' annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
    猜你喜欢
    • 2014-10-25
    • 2019-06-22
    • 2013-12-24
    • 2020-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多