2015-03-03 32 views
0

我想檢查我的應用程序緩存的位置始終在增加。 因此,我需要一種方法來隨時訪問有關當前緩存的信息。 我認爲必須有這樣一種方法,因爲我可以在我的android-smartphone的App-Info中看到緩存的大小。 該應用程序正在後臺運行,並且我想在緩存大小升高時進行日誌記錄。如何獲取有關應用程序緩存的代碼信息?

回答

1

使用下面的代碼片段爲您的解決方案。

public static byte[] retrieveData(Context context, String name) throws IOException { 

     File cacheDir = context.getCacheDir(); 
     File file = new File(cacheDir, name); 

     if (!file.exists()) { 
      // Data doesn't exist 
      return null; 
     } 

     byte[] data = new byte[(int) file.length()]; 
     FileInputStream is = new FileInputStream(file); 
     try { 
      is.read(data); 
     } 
     finally { 
      is.close(); 
     } 

     return data; 
    } 

下面是完整的的CacheManager類。

When to clear the cache dir in Android?

相關問題