title: Open file in cache directory with ACTION_VIEW
body: <p>I have been searching for this for a while now but it I can't make this to work correctly. Let me explain.</p>
  
  <p>I have an android application that saves files <strong>(images, documents, ...)</strong> in the cache directory. At first I used to <code>getExternalCacheDir()</code> method and save them over there but because it should be cached on devices that do not have an SD-card, I have to use <code>getCacheDir()</code>.</p>
  
  <p>When I used to <code>getExternalCacheDir()</code> method, it was no problem to open those files in another application like this:</p>
  
  <pre><code>Intent intent = new Intent();
  intent.setAction(Intent.ACTION_VIEW);
  intent.setDataAndType(Uri.fromFile(file), mimetype);
  </code></pre>
  
  <p>But when using the <code>getCacheDir()</code>, those files are saved in the application sandbox and are not accessible from outside the application. So I <strong>googled it and came on the **ContentProvider</strong>. A ContentProvider makes it possible to open private files with external applications. But when I try to implement it, it doesn't work.</p>
  
  <p>I tried implementing the ContentProvider thanks to this post: <a href="https://stackoverflow.com/questions/21304489/how-to-open-private-files-saved-to-the-internal-storage-using-intent-action-view">How to open private files saved to the internal storage using Intent.ACTION_VIEW?</a> but with no success.</p>
  
  <pre><code>package com.myapplication.providers;
  
  import java.io.File;
  import java.io.FileNotFoundException;
  
  import android.content.ContentProvider;
  import android.content.ContentValues;
  import android.database.Cursor;
  import android.net.Uri;
  import android.os.ParcelFileDescriptor;
  
  public class FileProvider extends ContentProvider {
  
      @Override
      public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
          File privateFile = new File(getContext().getCacheDir(), uri.getPath());
          return ParcelFileDescriptor.open(privateFile, ParcelFileDescriptor.MODE_READ_ONLY);
      }
  
      @Override
      public int delete(Uri arg0, String arg1, String[] arg2) {
          return 0;
      }
  
      @Override
      public String getType(Uri arg0) {
          return null;
      }
  
      @Override
      public Uri insert(Uri arg0, ContentValues arg1) {
          return null;
      }
  
      @Override
      public boolean onCreate() {
          return false;
      }
  
      @Override
      public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3,
              String arg4) {
          return null;
      }
  
      @Override
      public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
          return 0;
      }
  }
  </code></pre>
  
  <p>Added this provider to the application manifest</p>
  
  <pre><code>&lt;provider
      android:name="com.myapplication.providers.FileProvider"
      android:authorities="com.myapplication"
      android:exported="true" /&gt;
  </code></pre>
  
  <p>And using this code to open the file:</p>
  
  <pre><code>Uri uri = Uri.parse("content://com.myapplication/" + filename);
  
  Intent intent = new Intent();
  intent.setAction(Intent.ACTION_VIEW);
  intent.setDataAndType(uri, mimetype);
  </code></pre>
  
  <p>Thanks in advance!</p>