carlduke
carlduke

Reputation: 578

Sharing data on Android with Intent

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:

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)));

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:

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)));

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.

Why do other apps crash when trying to share an image?

Thanks for reply.


EDIT: I stored the image in the public picture directory, but apps like fb messenger trying to share it still crash!

This is the new 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();
  }

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)

Upvotes: 0

Views: 2947

Answers (2)

Drew
Drew

Reputation: 3334

/data/* path is not available to apps (security model of the filesystem every *nix user should be aware of).

The fact you are able to access this folder on your rooted device does not mean the app could do the same.

You could either store your image in publicly available directory (such as Pictures folder of the device, etc), or try to construct URI for your asset using the scheme:

android.resource://[package]/[res type]/[res name]

Upvotes: 1

Bishan
Bishan

Reputation: 15740

Try this.

private void shareIntent(String type,String text){
     File filePath = getFileStreamPath("shareimage.jpg");
     Intent shareIntent = new Intent();
     shareIntent.setAction(Intent.ACTION_SEND);
     shareIntent.putExtra(Intent.EXTRA_TEXT, text);
     shareIntent.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(new File(filePath)));  

     shareIntent.setType("image/*"); //"image/*" or "image/jpeg", etc
     shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
     startActivity(Intent.createChooser(shareIntent, "send"));
}

Upvotes: 0

Related Questions