title: Sharing cached images using FileProvider
body: <p>I have an app that uses <a href="https://github.com/nostra13/Android-Universal-Image-Loader/">Universal Image Loader</a> to download photos from the internet, cache them to data/data/com.myapp/cache and display in ImageViews.</p>
  
  <p>I also wanted to add sharing (WhatsApp, Facebook, Instagram, Dropbox etc.) to my app, so I tried to use this code:</p>
  
  <pre><code>Intent intent = new Intent();
  intent.setAction(Intent.ACTION_SEND);
  intent.putExtra(Intent.EXTRA_STREAM, photoUri);
  intent.setType("image/jpeg");
  startActivity(Intent.createChooser(intent, "Share image"));
  </code></pre>
  
  <p>photoUri was the uri in cache folder which other apps don't have permission to read.</p>
  
  <p>So I googled/stackoverflowed and <a href="http://developer.android.com/training/sharing/send.html">found out</a> that I need to use FileProvider.</p>
  
  <p>I configured the FileProvider as follows (<a href="http://developer.android.com/reference/android/support/v4/content/FileProvider.html">as was written here</a>) in my manifest:</p>
  
  <pre><code>&lt;provider
      android:name="android.support.v4.content.FileProvider"
      android:authorities="com.myapp.fileprovider"
      android:exported="false"
      android:grantUriPermissions="true"&gt;
      &lt;meta-data
          android:name="android.support.FILE_PROVIDER_PATHS"
          android:resource="@xml/file_paths" /&gt;
  &lt;/provider&gt;
  </code></pre>
  
  <p>And the file_paths.xml:</p>
  
  <pre><code>&lt;paths xmlns:android="http://schemas.android.com/apk/res/android"&gt;
      &lt;cache-path name="photo_cache" path="/"/&gt;
  &lt;/paths&gt;
  </code></pre>
  
  <p>Then I use the following code in my activity in button onClickListener:</p>
  
  <pre><code>File photoFile = ImageLoader.getInstance().getDiscCache().get("HTTP LINK HERE"); 
  // returns File of "/data/data/com.myapp/cache/-1301123243"
  Uri photoUri = FileProvider.getUriForFile(MyActivity.this, "com.myapp.fileprovider", photoFile);
  // returns Uri of "content://com.myapp.fileprovider/photo_cache/-1301123243"
  photoUri = Uri.parse(photoUri.toString() + ".jpg");
  // then I add .jpg to file name
  
  // Create intent
  Intent intent = new Intent();
  intent.setAction(Intent.ACTION_SEND);
  intent.putExtra(Intent.EXTRA_STREAM, photoUri);
  intent.setType("image/jpeg");
  
  // Grant permissions to all apps that can handle this intent
  // thanks to this answer http://stackoverflow.com/a/18332000
  List&lt;ResolveInfo&gt; resInfoList = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
  for (ResolveInfo resolveInfo : resInfoList) {
      String packageName = resolveInfo.activityInfo.packageName;
      grantUriPermission(packageName, photoUri, 
          Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
  }
  
  // And start
  startActivity(Intent.createChooser(intent, "Share image"));
  </code></pre>
  
  <p>However, depending on the app I'm getting strange exceptions. Like this:</p>
  
  <pre><code>1974-1974/com.whatsapp W/Bundle﹕ Key android.intent.extra.STREAM expected ArrayList but value was a android.net.Uri$HierarchicalUri.  The default value &lt;null&gt; was returned.
  1974-1974/com.whatsapp W/Bundle﹕ Attempt to cast generated internal exception:
  java.lang.ClassCastException: android.net.Uri$HierarchicalUri cannot be cast to java.util.ArrayList
          at android.os.Bundle.getParcelableArrayList(Bundle.java:1223)
          at android.content.Intent.getParcelableArrayListExtra(Intent.java:4425)
          at com.whatsapp.ContactPicker.d(ContactPicker.java:320)
          at com.whatsapp.ContactPicker.onCreate(ContactPicker.java:306)
          at android.app.Activity.performCreate(Activity.java:5104)
  </code></pre>
  
  <p>Or I just see logs like this:</p>
  
  <pre><code>591-963/system_process W/ActivityManager﹕ Permission denied: checkComponentPermission()
  </code></pre>
  
  <p>And of course apps themselves give me toasts with errors (that they can't open or download the file).</p>
  
  <p>I tried, googled, stackoverflowed A LOT, so now I'm posting here.
  I guess I'm doing it right, maybe just some little thing is wrong...</p>
  
  <p>Any help will be appreciated!</p>