How check if a DiskLruCache already exists? (Android)

前端 未结 1 1741
清酒与你
清酒与你 2020-12-22 10:12

I´m using that way of cache Bitmaps in my app Using DiskLruCache in android 4.0 does not provide for openCache method

Thing is that I´m using that line in onCreate()

相关标签:
1条回答
  • 2020-12-22 10:52

    Check if sd card is mounted. Get the path of the sdcard. Check if the folder under sdcard already exists, if not create one.

    Remember to add permission in manifest file

     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    if(android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED))
    {
      File cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"MyFolder");
    
        if(!cacheDir.exists())
            cacheDir.mkdirs();
    }
    

    You can use the below. Found this on developer site in the below link

    File cacheDir = getDiskCacheDir(ActivityName.this, "thumbnails");  
    if(!cacheDir.exists()) // check if it exits. if not create one
     {
        cacheDir.mkdirs(); 
     } 
    
    public static File getDiskCacheDir(Context context, String uniqueName) {
    // Check if media is mounted or storage is built-in, if so, try and use external cache dir
    // otherwise use internal cache dir
    final String cachePath =
            Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) ||
                    !isExternalStorageRemovable() ? getExternalCacheDir(context).getPath() :
                            context.getCacheDir().getPath();
    
    return new File(cachePath + File.separator + uniqueName);
    }
    

    For more information check the link below

    http://developer.android.com/training/displaying-bitmaps/cache-bitmap.html

    I see you have used getAppliactionContext(). Check the link below

    When to call activity context OR application context?. Get to know when to use activity context and getApplicationContext()

    Edit:

       File cacheDir=new File(android.os.Environment.getExternalStorageDirectory(),"MyFolder");
     if(!cacheDir.exists()) // check if it exits. if not create one
      {
       cacheDir.mkdirs(); 
       DiskLruImageCache dlic=new DiskLruImageCache(ActivityName.this,cacheDir, CACHESIZE, CompressFormat.PNG, 70);
      }
      else
       {
          DiskLruImageCache dlic=new DiskLruImageCache(ActivityName.this,cacheDir, CACHESIZE, CompressFormat.PNG, 70);
       } 
    

    Edit: 2

    As you can see below you are just passing the file not creating a new one.

    private DiskLruCache(File directory, int appVersion, int valueCount, long maxSize) {
        this.directory = directory;
        this.appVersion = appVersion;
        this.journalFile = new File(directory, JOURNAL_FILE);
        this.journalFileTmp = new File(directory, JOURNAL_FILE_TMP);
        this.valueCount = valueCount;
        this.maxSize = maxSize;
    }
    
    public static DiskLruCache open(File directory, int appVersion, int valueCount, long maxSize)
            throws IOException {
        if (maxSize <= 0) {
            throw new IllegalArgumentException("maxSize <= 0");
        }
        if (valueCount <= 0) {
            throw new IllegalArgumentException("valueCount <= 0");
        }
    
        // prefer to pick up where we left off
        DiskLruCache cache = new DiskLruCache(directory, appVersion, valueCount, maxSize);
        if (cache.journalFile.exists()) {
            try {
                cache.readJournal();
                cache.processJournal();
                cache.journalWriter = new BufferedWriter(new FileWriter(cache.journalFile, true),
                        IO_BUFFER_SIZE);
                return cache;
            } catch (IOException journalIsCorrupt) {
                 System.logW("DiskLruCache " + directory + " is corrupt: "
                        + journalIsCorrupt.getMessage() + ", removing");
                cache.delete();
            }
        }
    
        // create a new empty cache
        directory.mkdirs();
        cache = new DiskLruCache(directory, appVersion, valueCount, maxSize);
        cache.rebuildJournal();
        return cache;
    }
    
    0 讨论(0)
提交回复
热议问题