开发者

How to test if something is in cache i created?

I created a cache for my images here

            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);    

                    }  



                    }

No i would like to test if something is inside of the cache in my async Task.

Here is my async where i want to test if something is in there. The images i loaded before the application for killed.

 //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) {t开发者_StackOverflowry {
                            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;
                }


You can use the exists function on a file to check if a file exists or not. It returns false if the file does not exist.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜