简体   繁体   English

在 Android 上缓存

[英]Caching on Android

I have an app which gets data from Parse.com and uses in 2 sections, one in a listView and second in a grid view.我有一个应用程序,它从 Parse.com 获取数据并在 2 个部分中使用,一个在 listView 中,第二个在网格视图中。

So I want to cache this data(Strings,bitmaps),to work offline, whenever the device won't have internet so it loads from cache.所以我想缓存这个数据(字符串,位图),离线工作,只要设备没有互联网,它就从缓存加载。 And whenever my app closed and opened again, it should update the cached data if there is internet.每当我的应用程序关闭并再次打开时,如果有互联网,它应该更新缓存的数据。 During the update if I have the same data in my cache it shouldn't create it again, it has to only create the new ones and update the views.在更新期间,如果我的缓存中有相同的数据,则不应再次创建它,它只需要创建新数据并更新视图。

What is the best way to do this in android ?.在 android 中执行此操作的最佳方法是什么? Thanks.谢谢。

For caching a data coming form Webservice 用于缓存来自Webservice的数据

for Get a data u can use Volley or retrofit after getting an data you can set it to user desired attributes for caching the data both library giving the caching option for saving an data try to use that library. 对于获取数据,您可以在获取数据后使用Volley或翻新,您可以将其设置为用户所需的属性来缓存数据,两个库都提供了用于保存数据的缓存选项,请尝试使用该库。

To cache image you can use some library as: Glide, Picaso ... To cache text your need set up cache with HttpClient. 要缓存图像,您可以使用以下库:Glide,Picaso ...要缓存文本,您需要使用HttpClient设置缓存。 Example: 例:

private void configureCache() {
    if (cache == null)
        cache = createHttpClientCache(context);

    mHttpClient.setCache(cache);
    mHttpClient.networkInterceptors().add(REWRITE_CACHE_CONTROL_INTERCEPTOR);
}

private static Cache createHttpClientCache(Context context) {
    File cacheDir = context.getDir("cache_api", Context.MODE_PRIVATE);
    return new Cache(cacheDir, HTTP_CACHE_SIZE);
}

For String / Text : 对于字符串/文本:

  1. If your data amount is not so much , you can use SharedPreference 如果您的数据量不是很多,则可以使用SharedPreference

  2. If you have huge Data you should use SQlite 如果您有大量数据,则应使用SQlite

For Image : 对于图像:

  1. Picasso -> http://square.github.io/picasso/ 毕加索-> http://square.github.io/picasso/

  2. Universal Image Loader -> https://github.com/nostra13/Android-Universal-Image-Loader 通用图像加载器-> https://github.com/nostra13/Android-Universal-Image-Loader

try this code it will help- 试试这个代码将有帮助-

File cacheDir = getBaseContext().getCacheDir();
File f = new File(cacheDir, "pic");

try {
    FileOutputStream out = new FileOutputStream(
            f);
    pic.compress(
            Bitmap.CompressFormat.JPEG,
            100, out);
    out.flush();
    out.close();

} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

Intent intent = new Intent(
        AndroidActivity.this,
        OpenPictureActivity.class);
startActivity(intent);

and then in the new activity- 然后在新活动中

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.open_pic_layout);
    File cacheDir = getBaseContext().getCacheDir();
    File f = new File(cacheDir, "pic");     
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(f);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Bitmap bitmap = BitmapFactory.decodeStream(fis);

    ImageView viewBitmap = (ImageView) findViewById(R.id.icon2);
    viewBitmap.setImageBitmap(bitmap);

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

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