我知道有这样的解决方案,人们说使用glide或picasso这样的图像缓存工具,可以显示图像。但我也试过了,不会成功的。
下面是我将文件上传到firebase存储的实现

 if (uri != null) {
        //upload to storage
        StorageReference riversRef = imagesRef.child(uri.getLastPathSegment());
        UploadTask uploadTask = riversRef.putFile(uri);
        // Register observers to listen for when the download is done or if it fails
        uploadTask.addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                // Handle unsuccessful uploads
                Snackbar.make(coordinatorLayout, "Unable to process your request. Please try again.", Snackbar.LENGTH_SHORT).show();
            }
        }).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                // taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
                Uri downloadUrl = taskSnapshot.getMetadata().getDownloadUrl();
                if (downloadUrl != null) {
                    Date date = new Date();
                    Locale locale = new Locale("en", "IN");
                    SimpleDateFormat sdf = new SimpleDateFormat("MM-dd-yyyy /h:mm a", locale);
                    String formattedDate = sdf.format(date);

                    HashMap<String, String> map = new HashMap<>();
                    map.put("isXRead", String.valueOf(false));
                    map.put("isYRead", String.valueOf(true));
                    map.put("imgUrl", String.valueOf(downloadUrl));
                    map.put("timestamp", formattedDate);
                    map.put("user", "X");
                    ref.push().setValue(map);
                }
            }
        });
    }

图像上传良好并返回url,类似于
https://firebasestorage.googleapis.com/v0/b/private.appspot.com/o/Messenger_images%2F14076?alt=media&token=2164ec58-55c2-4b03-b97a-ebfd36e66593

下面是我如何使用截击显示图像:
ImageRequest ir = new ImageRequest(message.getImgUrl(), new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                // callback
                // new GlideOperations(context, img).glideMessenger(response);
                img.setImageBitmap(response);
            }
        }, 100, 100, null, null);
        //ir.setShouldCache(false);
        AppController.getInstance().addToRequestQueue(ir);

下面是我使用glide显示图像的方法:
 try {
        Glide.with(mContext)
                .load(object)
                .thumbnail(0.5f)
                .override(900, 400)
                .crossFade(100)
                .fitCenter()
                .error(R.drawable.wallpaper)
                .into(new SimpleTarget<GlideDrawable>() {
                    @Override
                    public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                        try {
                            mImageView.setImageDrawable(resource);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    } catch (Exception e) {
        e.printStackTrace();
    }

以上这些似乎都不起作用。谢谢你的帮助。
解决方案
似乎我对我使用的ImageView有问题。谢谢你的帮助。

最佳答案

com.firebaseui:firebase-ui-storage:0.6.0库包括使用glide加载firebase存储图像的功能。

// 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);

10-07 19:26
查看更多