title: File caching and file permission
body: <p>I have a problem implementing caching in my app and deploying it on 4.2.x devices.</p>
  
  <p>When i save a file in the app cache directory i'm unable to read it. I use a file manager with  root permissions to explore cache folder and the <strong>files are correctly present in my cache folder but when i try to read them a get a file not found error</strong>. </p>
  
  <p><strong><em>This problem is related only to 4.2 android version. In 4.1- all works fine</em></strong></p>
  
  <p>This is the code i use to write the file</p>
  
  <pre><code>public static void writeFile(Bitmap bmp, File f) {
  
          if (bmp == null)
              return;
  
          FileOutputStream out = null;
  
          try {
              out = new FileOutputStream(f);
              boolean res = bmp.compress(Bitmap.CompressFormat.PNG, 80, out);
  
          } catch (Exception e) {
              e.printStackTrace();
          } finally {
              try {
                  if (out != null)
                      out.close();
              } catch (Exception ex) {
              }
          }
          f.setReadable(true, false);
      }
  </code></pre>
  
  <p>This is how i construct the file object</p>
  
  <pre><code>File bitmapFile = new File(ImageManager.getInstance().getCacheDir(), key);
  </code></pre>
  
  <p>This is how get the cache path</p>
  
  <pre><code>if (Config.USE_INTERNAL_CACHE == false &amp;&amp; sdState.equals(android.os.Environment.MEDIA_MOUNTED)) {
          cacheDir = MyApp.getInstance().getExternalCacheDir();
      } else {
          cacheDir = MyApp.getInstance().getCacheDir();
  
      }
      if (!cacheDir.exists()) {
          cacheDir.mkdirs();
      }
  </code></pre>
  
  <p>At this point my file are correctly written to </p>
  
  <pre><code>/data/data/my.app.package/cache/
  </code></pre>
  
  <p>This is the code i use to read the files</p>
  
  <pre><code>public BitmapDrawable readFile(String fileName) {
      FileInputStream in = null;
      try {
          in = new FileInputStream(getCacheDir()+"/"+fileName);
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      }
      Bitmap bmp = BitmapFactory.decodeStream(in);
  
      if (bmp == null) {
          return null;
      }
      return new BitmapDrawable(MyApp.getInstance().getResources(), bmp);
  }
  </code></pre>
  
  <p>This always generate this error: </p>
  
  <pre><code>01-22 11:23:47.158: W/System.err(20660): java.io.FileNotFoundException: /data/data/my.app.package/cache/File Name: open failed: ENOENT (No such file or directory)
  01-22 11:23:47.158: W/System.err(20660):    at libcore.io.IoBridge.open(IoBridge.java:416)
  01-22 11:23:47.158: W/System.err(20660):    at java.io.FileInputStream.&lt;init(FileInputStream.java:78)
  01-22 11:23:47.158: W/System.err(20660):    at java.io.FileInputStream.&lt;init(FileInputStream.java:105)
  01-22 11:23:47.158: W/System.err(20660):    atmy.app.package.instanceManager.ImageManager.readFile(ImageManager.java:246)
  01-22 11:23:47.158: W/System.err(20660):    atmy.app.package.instanceManager.ImageManager$ImageQueueManager.run(ImageManager.java:186)
  01-22 11:23:47.158: W/System.err(20660):    at java.lang.Thread.run(Thread.java:856)
  01-22 11:23:47.158: W/System.err(20660): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
  01-22 11:23:47.158: W/System.err(20660):    at libcore.io.Posix.open(Native Method)
  01-22 11:23:47.158: W/System.err(20660):    atlibcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
  01-22 11:23:47.158: W/System.err(20660):    at libcore.io.IoBridge.open(IoBridge.java:400)
  01-22 11:23:47.158: W/System.err(20660):    ... 5 more
  </code></pre>
  
  <p>related to this line:</p>
  
  <pre><code>in = new FileInputStream(getCacheDir()+"/"+fileName);
  </code></pre>
  
  <p>I have already added this permission</p>
  
  <pre><code>&lt;uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /&gt;
  </code></pre>
  
  <p>The path and the name file related to this error are correct and the file exists in the cache folder, so why i cannot read them? **I think it could be an user permission related issue due to the new multiuser features introduced in android 4.2 but i cant figure it out. **I also try to set the file <code>.setReadable(true, false)</code> but nothing. Thanks</p>