首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >避免使用Recycleview从Firebase重新加载图像

避免使用Recycleview从Firebase重新加载图像
EN

Stack Overflow用户
提问于 2020-04-22 06:38:15
回答 2查看 41关注 0票数 0

我有一个循环视图,它加载存储在Firebase存储中的不同图像。当我滚动我的循环视图时,它每次都加载相同的图像(就像循环视图的定义一样)。

如何下载这些图像一次,并附加到我的循环视图,使它不应该重新加载时,滚动?

我试过这样做。

代码语言:javascript
复制
public void onBindViewHolder(final TodaysBdayViewHolder holder, int position)
{
           storageReference=FirebaseStorage.getInstance().getReference().child("profiles/"+contacts.get(position)+".jpg");

            final File localFile=File.createTempFile("profile_pic","jpeg",new File(context.getExternalFilesDir("null").getAbsolutePath()));

            storageReference.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
                public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {

                    Log.i("app","FinishIntro Img loaded");
                    Bitmap bitmap= BitmapFactory.decodeFile(localFile.getAbsolutePath());
                    holder.FriendPhoto.setImageBitmap(bitmap);
                }
            }).addOnFailureListener(new OnFailureListener() {

                public void onFailure(@NonNull Exception e) {

                }
            });
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-04-22 07:04:19

您可以使用滑翔缓存图像并在图像视图中显示它。

代码语言:javascript
复制
public void onBindViewHolder(final TodaysBdayViewHolder holder, int position)
{
    storageReference = FirebaseStorage.getInstance().getReference().child("profiles/"+contacts.get(position)+".jpg");
    // get the download URL
    storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
        public void onSuccess(Uri uri) {
            // load and cache with glide
            Glide.with(context)
                .load(uri)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(holder.FriendPhoto);
        }
    }).addOnFailureListener(new OnFailureListener() {

        public void onFailure(@NonNull Exception e) {

        }
    });
}

缓存文档

票数 0
EN

Stack Overflow用户

发布于 2020-04-22 06:54:41

您正在从文件中自己解码位图,这不是很有效。相反,我建议使用一个库来高效地完成类似毕加索滑行这样的事情。

毕加索方法:

添加到gradle:

代码语言:javascript
复制
implementation 'com.squareup.picasso:picasso:2.71828'

并在加载时执行此操作:

代码语言:javascript
复制
//the file  
final File localFile=File.createTempFile("profile_pic","jpeg",new File(context.getExternalFilesDir("null").getAbsolutePath()));


//the reference
storageReference=FirebaseStorage.getInstance().getReference().child("profiles/"+contacts.get(position)+".jpg");

//the call
storageReference.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
    @Override
    public void onSuccess(Uri uri) {
    // use this uri in picasso call into imageview
Picasso.get().load(uri.toString()).into(holder.FriendPhoto);

    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // Handle any errors
    }
});
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61358724

复制
相关文章

相似问题

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