繁体   English   中英

从谷歌firebase存储缓存本地图像

[英]Cache images local, from google firebase storage

我正在寻找一种方法,从谷歌firebase平台上的存储缓存图像。 现在,我可以下载图像,并向用户显示这些图像,但即使没有互联网连接,我也无法缓存此图像并进行访问。 可以脱机访问数据库。 所以我想,还有一种存储方式。 我不想将每个图像下载到存储器,因此我需要每次检查,如果图像仍然是最新的,则可能会更改。 这里有一些链接,我能找到的,但我的问题没有答案。 也许有人知道一种解决方法,或者一种如何实现它的方法。 谢谢!

下载文件: https//firebase.google.com/docs/storage/android/download-files

缓存(离线)数据库: https//firebase.google.com/docs/database/android/offline-capabilities

更新1

这是我用毕加索“缓存”文件的方式,我添加了关注下载的活动:

Picasso.with(getApplicationContext())
                            .load(uri.toString())
                            .networkPolicy(NetworkPolicy.OFFLINE)
                            .into(image1);

欢迎任何帮助。 谢谢!

我担心Firebase SDK本身不提供图像缓存。 但是有几个很棒的库可以帮到你。 他们下载图像,在ImageView中显示它并将其缓存在一行代码中。 只需向Firebase请求图片下载网址并将其提供给图片缓存库。

如果您选择Picasso作为缓存库,那么这样的事情:

storageRef.child("users/me/profile.png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
    @Override
    public void onSuccess(Uri uri) {
        // Got the download URL for 'users/me/profile.png'
        // Pass it to Picasso to download, show in ImageView and caching
        Picasso.with(context).load(uri.toString()).into(imageView);
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // Handle any errors
    }
});

UPD:要使用Picasso进行磁盘缓存,您需要显式设置OkHttpDownloader。 在这里查看如何在Picasso中使用磁盘缓存?

感谢@ATom的通知。 Api已更改,FirebaseUI 3.0现在使用Glide 4.x这是更新的示例:

要从StorageReference加载图像,请首先在AppGlideModule中注册:

 @GlideModule public class MyAppGlideModule extends AppGlideModule { @Override public void registerComponents(Context context, Glide glide, Registry registry) { // Register FirebaseImageLoader to handle StorageReference registry.append(StorageReference.class, InputStream.class, new FirebaseImageLoader.Factory()); } } 

然后,您可以将StorageReference加载到ImageView中:

 // Reference to an image file in Cloud Storage StorageReference storageReference = ...; // ImageView in your Activity ImageView imageView = ...; // Download directly from StorageReference using Glide // (See MyAppGlideModule for Loader registration) GlideApp.with(this /* context */) .load(storageReference) .into(imageView); 

并且不要忘记在build.gradle添加依赖build.gradle

implementation 'com.firebaseui:firebase-ui-:3.1.0'

在GitHub上回答消息来源

老答案:

FirebaseUI 1.0现已发布。 存储示例具有类FirebaseImageLoader

使用FirebaseImageLoader显示的图像由其在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);

我遇到过同样的问题。 尝试了所有可能的方法,但无法解决这个问题。 我认为问题在于firebase存储生成的链接。 毕加索通常会缓存它加载的图像。 我尝试了其他云服务,如cloudinary,LibPixel,其链接以图像格式(例如:.jpg)结束,毕加索缓存这些链接图像。 虽然firebase链接以令牌号结束。 奇怪的是它失败了!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM