title: Get Cache Directory for all installed apps in android
body: <p>I want to get exact size of cache directory of android installed apps. I have tried this.</p>
  
  <pre><code>List&lt;ApplicationInfo&gt; listOfApps = packageManager.getInstalledApplications(PackageManager.GET_META_DATA);
       for(ApplicationInfo applicationInfo : listOfApps) {
  
           try {
  
               Context context = createPackageContext(applicationInfo.packageName,CONTEXT_IGNORE_SECURITY);
               File file = context.getCacheDir();
               if(file!=null){
               String path = file.getAbsolutePath();
  
               long dir = file.length();
               Log.d("All_Package", applicationInfo.packageName + "\t\t" + MemoryFreedPredication.formatFileSize(String.valueOf(dir)) + " \t\t"+path);}
           } catch (PackageManager.NameNotFoundException e) {
               e.printStackTrace();
           }
  </code></pre>
  
  <p>Method to to format size is </p>
  
  <pre><code>public static String formatFileSize(String fileSize) {
  
      String[] sizes = {"B", "KB", "MB", "GB"};
      double len = Double.parseDouble(fileSize);
      int order = 0;
  
      if (len &lt; 1024) {
          return String.format("%s %s", 1, sizes[1]);
      } else {
          while (len &gt;= 1024 &amp;&amp; order + 1 &lt; sizes.length) {
              order++;
              len = len / 1024;
          }
      }
  
      len = Math.round(len * 100.0) / 100.0;
      return String.format("%s %s", len, sizes[order]);
  }
  </code></pre>
  
  <p>But it is always returns 4Kb</p>