1

i want to create a button and when the button is clicked share an image. I found that i could use this:

final Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jpg");
final File photoFile = new File(getFilesDir(), "photo.jpg");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(photoFile));
startActivity(Intent.createChooser(shareIntent, "Share image using")); 

but i dont know where to put it(MainActivity?) i would like to share the image in WhatsApp.

  • Add this code to **OnClickListener** function when u click on Button – Danh DC Jan 04 '16 at 02:18
  • @DanhDC i put the OnClickListener but i cant make it work public void buttonOnClick(View v){ final Intent shareIntent = new Intent(Intent.ACTION_SEND); shareIntent.setType("image/jpg"); final File photoFile = new File(getFilesDir(), "ic_launcher.png"); shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(photoFile)); startActivity(Intent.createChooser(shareIntent, "Share image using")); } – knownothingjonsnow Jan 04 '16 at 03:38
  • Any have error occur when you click this button. Show your logcat if have error. Please make sure you implement right **OnClickListener** like this : **yourButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //put share action here } });** – Danh DC Jan 04 '16 at 03:45
  • @DanhDC Thank you man i made it work. I only have one more question i downloaded the apk to my phone but when i send the image it says that the image is not on the device. What can i do? – knownothingjonsnow Jan 04 '16 at 04:04
  • Because, the image **ic_launcher.png** is not visible in your real device, please change it to exist image you have in your phone. – Danh DC Jan 04 '16 at 04:06
  • @DanhDC And what if i pass my PC image to my phone? – knownothingjonsnow Jan 04 '16 at 04:11
  • Yes, you can do it, but please make sure your image path is correct. It would be better if you pick image from gallery like this http://www.itcuties.com/android/pick-image-from-gallery/ . Then, use this image path to send. Hope it useful for you – Danh DC Jan 04 '16 at 04:17

2 Answers2

0

You have to set: shareIntent.setPackage("com.whatsapp") to share in whatsapp.

Check this post for more information: sharing image with whatsapp in android

Community
  • 1
  • 1
0

you can refer below link

Sharing on Android

Adding an Easy Share Action

How to activate “Share” button in android app?

Community
  • 1
  • 1
caopeng
  • 914
  • 13
  • 23