0

i want share some image from res/drawable folder in my program . this is my code :

String _ImageFile = "android.resource://" + getResources().getResourceName(R.id.mypic).replace(":", "/");
Uri imageUri = Uri.parse(_ImageFile);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/jpeg");
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(intent, "Share"));

it's work with Instagram but not work with Email or Viber . when i select share with email , attached successfull but send file without any extention . what is my wrong ?

Mr.Milad
  • 57
  • 2
  • 12

4 Answers4

3

replace this line in your coding

intent.setType("image/*");

instead of

intent.setType("image/jpeg");
2

Other applications cannot see/read/write your private files in internal memory. 'Files' from res/drawable are also private. You have to copy them first to a place where external apps can 'reach' them. To external memory for instance.

greenapps
  • 11,154
  • 2
  • 16
  • 19
0

Try -

String _ImageFile = "android.resource://" + getResources().
getResourceName(resID).replace(":", "/");
Uri imageUri = Uri.parse(_ImageFile);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(intent, "Share"));

For Viber -

String _ImageFile = "android.resource://" + getResources().
getResourceName(resID).replace(":", "/");
Uri imageUri = Uri.parse(_ImageFile);

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
intent.setPackage("com.viber.voip"); 
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
startActivity(Intent.createChooser(intent, "Share"));

UPDATE:

To get the image URI for drawable Images, do like this -

Uri uri = Uri.parse("android.resource://your package name/"
          +R.drawable.image_name);
intent.putExtra(Intent.EXTRA_STREAM, uri);
sjain
  • 23,126
  • 28
  • 107
  • 185
  • @MiladCoder - Check the update. You need to use package for viber which is `"com.viber.voip".` – sjain Jun 30 '14 at 07:29
  • i have't problem with Viber Intent . but i have problem with image extention . i guess send image to viber without any extention . – Mr.Milad Jun 30 '14 at 07:31
  • @MiladCoder - Did you tried the way I mentioned ? Again you try with `com.viber.voip` and also set the intent type as `image/*`. – sjain Jun 30 '14 at 07:32
  • i want send file to all of sharing Intent . your code navigate to JUST viber app . and not work yet... – Mr.Milad Jun 30 '14 at 07:36
  • @MiladCoder - Your first comment was for `Viber` with the intention that rest other things are working. So my second update in the code was for `Viber`. What is not working. Please mention. – sjain Jun 30 '14 at 07:40
  • when i click on share icon . appear intent sharing with some app . and select one app to share . this code just working with Instagram . not working whit viber or email or..... – Mr.Milad Jun 30 '14 at 07:50
  • @MiladCoder - Are you sure that your image is a valid jpg file. Are you able to open it manually ? This code is just like http://stackoverflow.com/questions/23855941/share-image-does-not-work-in-viber-and-facebook. It should work. – sjain Jun 30 '14 at 07:55
0

See the documentation and mind this:

Note: Some e-mail applications, such as Gmail, expect a String[] for extras like EXTRA_EMAIL and EXTRA_CC, use putExtra(String, String[]) to add these to your intent.

So, for example add the additional extras like this:

intent.putExtra(Intent.EXTRA_SUBJECT, “Some Subject Line”);

And consider which extras your target applications expect.

12oz Mouse
  • 61
  • 4
  • i have problem with image extention . when i select email , attach file without any extention . and same problem in viber – Mr.Milad Jun 30 '14 at 07:23
  • Then browse this issues: http://stackoverflow.com/questions/6304082/gmail-attachment-and-custom-extension http://stackoverflow.com/questions/13870570/custom-mime-type-ignored-in-gmail-app – 12oz Mouse Jun 30 '14 at 07:37
  • use less url . you know what ? i have problem with image extention for sharing... – Mr.Milad Jun 30 '14 at 07:46