コード例 #1
0
 /**
  * Initializes the disk cache. Note that this includes disk access so this should not be executed
  * on the main/UI thread. By default an ImageProvider does not initialize the disk cache when it
  * is created, instead you should call initDiskCache() to initialize it on a background thread.
  */
 public void initDiskCache() {
   if (DEBUG) {
     Log.d(TAG, "initDiskCache " + this);
   }
   // Set up disk cache
   synchronized (mDiskCacheLock) {
     if (mDiskLruCache == null || mDiskLruCache.isClosed()) {
       if (mDiskCacheDir != null) {
         if (!mDiskCacheDir.exists()) {
           mDiskCacheDir.mkdirs();
         }
         if (FileUtil.getUsableSpace(mDiskCacheDir) > mDiskCacheSize) {
           try {
             mDiskLruCache = DiskLruCache.open(mDiskCacheDir, 1, 1, mDiskCacheSize);
             if (DEBUG) {
               Log.d(TAG, "Disk cache initialized " + this);
             }
           } catch (final IOException e) {
             Log.e(TAG, "initDiskCache - " + e);
           }
         } else {
           Log.e(
               TAG,
               String.format(
                   "no enough space for initDiskCache %s %s",
                   FileUtil.getUsableSpace(mDiskCacheDir), mDiskCacheSize));
         }
       }
     }
     mDiskCacheStarting = false;
     mDiskCacheReady = true;
     mDiskCacheLock.notifyAll();
   }
 }
コード例 #2
0
 /**
  * Closes the disk cache associated with this ImageProvider object. Note that this includes disk
  * access so this should not be executed on the main/UI thread.
  */
 public void closeDiskCache() {
   synchronized (mDiskCacheLock) {
     if (mDiskLruCache != null) {
       try {
         if (!mDiskLruCache.isClosed()) {
           mDiskLruCache.close();
           mDiskLruCache = null;
           if (DEBUG) {
             Log.d(TAG, "Disk cache closed");
           }
         }
       } catch (IOException e) {
         Log.e(TAG, "close - " + e);
       }
     }
   }
 }
コード例 #3
0
  /**
   * Clears both the memory and disk cache associated with this ImageProvider object. Note that this
   * includes disk access so this should not be executed on the main/UI thread.
   */
  public void clearCache() {

    synchronized (mDiskCacheLock) {
      mDiskCacheStarting = true;
      mDiskCacheReady = false;

      if (mDiskLruCache != null && !mDiskLruCache.isClosed()) {
        try {
          mDiskLruCache.delete();
          if (DEBUG) {
            Log.d(TAG, "Disk cache cleared");
          }
        } catch (IOException e) {
          e.printStackTrace();
          Log.e(TAG, "clearCache - " + e);
        }
        mDiskLruCache = null;

        initDiskCache();
      }
    }
  }