title: Sharing data on Android with Intent
body: <p>I'm having a problem with sharing content on android between apps, with Intent
  I can successfully share plain text (on G+, Facebook and Facebook Messenger) 
  The code is the following: </p>
  
  <pre><code>Intent shareIntent = new Intent();
  shareIntent.setAction(Intent.ACTION_SEND);
  shareIntent.putExtra(Intent.EXTRA_TEXT,"Your score is: "+Integer.toString(mScore));
  shareIntent.setType("text/plain");
  startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));
  </code></pre>
  
  <p>But when it comes to images things don't work. 
  Intent is created and I can actually choose between the apps to share the image with, but when the selected app tries to start, it just crashes and doesn't open.
  No app works (tested with 9Gag, G+, Facebook, Facebook Messenger, Photos..) 
  The code I'm using is this: </p>
  
  <pre><code>Intent shareIntent = new Intent();
  shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
  shareIntent.setAction(Intent.ACTION_SEND);
  shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
  shareIntent.setType("image/png");
  startActivity(Intent.createChooser(shareIntent, getResources().getText(R.string.send_to)));
  </code></pre>
  
  <p>The image is stored on
       /data/data/com.test.carloalberto.myapp/files/score.png
  which is the right path where i previously saved the image.</p>
  
  <p>Why do other apps crash when trying to share an image? </p>
  
  <p>Thanks for reply.</p>
  
  <hr>
  
  <p><strong>EDIT</strong>: I stored the image in the public picture directory, but apps like fb messenger trying to share it still crash! </p>
  
  <p>This is the new code: </p>
  
  <pre><code>File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
  String name = "score.jpg";
  
  File file = new File(dir,name);
  FileOutputStream ostream;
  
   try {
          if(!dir.exists()) {
             Log.i("Share: onClick()","Creating directory "+dir);
             dir.mkdirs();
          }
  
        Log.i("Share: onClick()","Final path: "+file.getAbsolutePath());
        file.createNewFile();
  
        ostream = new FileOutputStream(file);
        Log.i("onClick(): Share action: ","Path: "+file.getAbsolutePath());
  
        bitmap.compress(Bitmap.CompressFormat.JPEG,100,ostream);
        ostream.close();
  
  
        Uri uriToImage = Uri.parse(file.getAbsolutePath());
        Log.i("onClick(): Share action, final uri ",uriToImage.toString());
  
        Intent shareIntent = new Intent();
        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, uriToImage);
        shareIntent.setType("image/jpeg");
        startActivity(Intent.createChooser(shareIntent,getResources().getText(R.string.send_to)));
  
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e){
        e.printStackTrace();
    } catch (Exception e){
        e.printStackTrace();
    }
  </code></pre>
  
  <p>Now the file path is: /mnt/sdcard/Pictures/score.jpg, which should be public and accessible by all other applications. 
  (Obviously I also set the permissions in the manifest file) </p>