如何测试缓存目录?

时间:2011-08-06 05:09:28

标签: android

我使用扩展基类的imageAdapter下载图像。我将它们存储在缓存中 这是我测试它们是否在缓存中的地方

            URI imageUri = null;

            //Setting the Uri of aURL to imageUri.
            try {
            imageUri = aURL.toURI();

            } catch (URISyntaxException e1) {
            // TODO Auto-generated catch block
                e1.printStackTrace();
                        }



                //Testing to see if images are already in cache, if not then we load the images from the web and save them to the cache.
           if (new File(new File(myContext.getCacheDir(), "thumbnails"), "" + imageUri.hashCode()).exists())
                           {

        Log.v("Loader", "File exists in cache. Now pulling from the cache");

        String cachFile = myContext.getCacheDir() +"/thumbnails/"+imageUri.hashCode();
        FileInputStream fis;

我的问题是如何在应用程序中的另一种方法中测试此缓存目录“缩略图?”

1 个答案:

答案 0 :(得分:1)

要测试文件是否在缓存目录中,请使用

File f = new File("<path_to_your_file>");
f.exists(); //TRUE if the file exists, FALSE otherwise.

在您的情况下,您可以在其他方法上使用相同的代码,因为myContext.getCacheDir()将在整个应用程序中返回相同的目录。

但是,在其他方法中,你仍然需要知道imageUri进行测试。

相关问题