android - networkimageview 在离线模式下显示图像?杀死应用程序并重新打开后。就像 Facebook 应用程序一样

标签 android android-volley networkimageview

我引用了 android 开发者网站并创建了带有单例类的 volley NetworkImageView。但是在强制关闭应用程序并再次离线打开我的应用程序后,它不会维护缓存图像。如何使网络 ImageView 在离线模式下从缓存中获取图像。我正在使用 LruCache。

注意:我在整个应用程序中使用 NetworkImageView。我在某处读到只有当图像 URL 包含缓存 header 时才会存储磁盘缓存。我想知道这一点,如果 URL 中没有这样的 header ,那么如何强制 volley 存储图像的磁盘缓存?

我的代码:

public class VolleySingletonPattern {

private static VolleySingletonPattern mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mCtx;

private VolleySingletonPattern(Context context) {
    mCtx = context;
    mRequestQueue = getRequestQueue();

    mImageLoader = new ImageLoader(mRequestQueue, new LruBitmapCache(
            LruBitmapCache.getCacheSize(mCtx)));
}

public static synchronized VolleySingletonPattern getInstance(Context context) {
    if (mInstance == null) {
        mInstance = new VolleySingletonPattern(context);
    }
    return mInstance;
}

public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        // getApplicationContext() is key, it keeps you from leaking the
        // Activity or BroadcastReceiver if someone passes one in.
        mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
    }
    return mRequestQueue;
}

public <T> void addToRequestQueue(Request<T> req) {
    getRequestQueue().add(req);
}

public ImageLoader getImageLoader() {
    return mImageLoader;
}}

我的 LruCache:

public class LruBitmapCache extends LruCache<String, Bitmap>
    implements ImageCache {

public LruBitmapCache(int maxSize) {
    super(maxSize);
}

public LruBitmapCache(Context ctx) {
    this(getCacheSize(ctx));
}

@Override
protected int sizeOf(String key, Bitmap value) {
    return value.getRowBytes() * value.getHeight();
}

@Override
public Bitmap getBitmap(String url) {
    return get(url);
}

@Override
public void putBitmap(String url, Bitmap bitmap) {
    put(url, bitmap);
}

// Returns a cache size equal to approximately three screens worth of images.
public static int getCacheSize(Context ctx) {
    final DisplayMetrics displayMetrics = ctx.getResources().
            getDisplayMetrics();
    final int screenWidth = displayMetrics.widthPixels;
    final int screenHeight = displayMetrics.heightPixels;
    // 4 bytes per pixel
    final int screenBytes = screenWidth * screenHeight * 4;

    return screenBytes * 3;
}}

最佳答案

我用过这个this ,并且正在为我工​​作。 您需要添加这些权限。

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

关于android - networkimageview 在离线模式下显示图像?杀死应用程序并重新打开后。就像 Facebook 应用程序一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35238131/

相关文章:

Android Volley 发布请求。如何设置实体?

android - 带有 Volley NetworkImageView 图像的水平 recyclerView 未显示

android - 向后兼容的 BackupAgent

java - 如何从 Youtube Api 获取 channel ID?它显示此错误 "JSONException: No value for channelId"

android - 如何在android中使音频扬声器静音

android - Messenger Service 可以用于跨应用程序 IPC 吗?

android - 传递 Volley 图像作为下一个 Activity 背景图像

java - JsonArrayRequest 没有给出任何响应