[Small note] Android Cache Issues

Source: Internet
Author: User

This evening, the product manager called to say that our Android app in addition to the problem, the problem is very simple is a cache problem, because this program is written by former colleagues, I can only lol, some things you have to carry. Back to the point, this cache problem, it is a little wonderful, so I want to record, hope to avoid

Problem

Read the code, feel no problem, but for the user's problems, or think this logic is wrong:

1 file cache is not on request network

Because the requested interface returned large data, do a file cache to local, this is true, but after the cache, when the next time in the request, actually first determine whether the cache file exists, if there is not read the network data, but directly with the cache file data —————— you can guarantee that, Is the data you're caching not changing? Can you guarantee that the data you are caching is correct?

2 when you cache a file, put it under SDcard

Cache files generally provided by the system has a relevant directory, when the application is uninstalled, the cache directory is not there, which saves users a lot of storage space. But what do you mean by putting it on an SD card? Uninstalled, this file is still in!!!!!.

Solutions

Problem has, it should be solved, each access to the network to read the network data, verify that the data is correct, only the correct cache.

The cache file is placed in the cache directory, and for this reason, a class for the Bitmapfun project is borrowed, as well:

Import Android.annotation.targetapi;import android.content.context;import android.os.environment;/** * It contains some helper classes about the path * @author cyning * @date 2014-7-10 a.m. 9:57:12 */public class Pathutil {/** * Get a usable cache D     Irectory (external if available, internal otherwise).  * * @param context the context to use * @param uniqueName A unique directory name to append to the cache dir * @return the Cache dir */public static File Getdiskcachedir (context context, String UniqueName) {//Check If media is mounted or storage are built-in, if so, try and use external cache dir//otherwise use internal cache D IR final String CachePath = Environment.MEDIA_MOUNTED.equals (Environment.getexternalstoragestate ())                        || !isexternalstorageremovable ()?        Getexternalcachedir (context). GetPath (): Context.getcachedir (). GetPath ();  return new File (CachePath + file.separator + uniqueName);  }/** * Check If external storage is built-in or removable.     * * @return True If external storage is removable (as an SD card), false * otherwise.            */@TargetApi (9) public static Boolean isexternalstorageremovable () {if (Compatutils.hasgingerbread ()) {        return environment.isexternalstorageremovable ();    } return true;            } @TargetApi (8) public static File Getexternalcachedir (context context) {if (Compatutils.hasfroyo ()) {        return Context.getexternalcachedir (); }//Before Froyo we need to construct the external cache dir ourselves final String Cachedir = "/android/da        ta/"+ context.getpackagename () +"/cache/";    return new File (Environment.getexternalstoragedirectory (). GetPath () + Cachedir); }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.