如何测试我创建的缓存中是否存在某些内容?

时间:2011-07-29 04:47:53

标签: android

我在这里为我的图片创建了一个缓存

            public void putBitmapInDiskCache(URI imageUri, Bitmap avatar) {   
                    File cacheDir = new File(this.getCacheDir(), "thumbnails");
                    cacheDir.mkdirs();
                    File cacheFile = new File(cacheDir, ""+imageUri.hashCode());   
                    try {      
                    cacheFile.createNewFile();       
                    FileOutputStream fos = new FileOutputStream(cacheFile);    
                    avatar.compress(CompressFormat.PNG, 100, fos);       
                    fos.flush();       
                    fos.close();    
                    } catch (Exception e) {       
                    Log.e("error", "Error when saving image to cache. ", e);    

                    }  



                    }

不,我想测试我的异步任务中是否存在某些内容。

这是我的异步,我想测试是否有东西在那里。我在申请杀人之前加载的图片。

 //the important AsyncTask method. running the background thread to get the images and set them to the gallery.
                private class MyTask extends AsyncTask<Void, Void, Void>{


                @Override
                protected Void doInBackground(Void... arg0) {try {
                            getImages();
                            Log.v("MyTask", "Image 1 retreived");
                            getImage2();
                            Log.v("MyTask", "Image 2 retreived");
                            getImage3();
                            Log.v("MyTask", "Image 3 retreived");
                            getImage4();
                            Log.v("MyTask", "Image 4 retreived");
                        } catch (IOException e) {
                            Log.e("MainMenu retreive image", "Image Retreival failed");
                            e.printStackTrace();
                        }
                    return null;
                }

1 个答案:

答案 0 :(得分:2)

您可以对文件使用exists函数来检查文件是否存在。如果文件不存在,则返回false。