0

I am trying to send image using intent as below :

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(mArray[position]));
shareIntent.setType("image/png");
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(shareIntent);

EDIT: FILE IMAGE ARRAY

private void createDrawbleArray(File fileDir) {

    mArray = new File[Constants.TOTAL_GRID_ICONS];
    TypedArray tArray = getResources().obtainTypedArray(R.array._images);

    for (int i = 0; i < Constants.TOTAL_GRID_ICONS; i++) {
        mArray[i] = getFileForResource(this, tArray.getResourceId(i, -1), fileDir, "a" + i + ".png");
    }
    tArray.recycle();

}

EDIT : PERMISSIONS

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />

But, unfortunately its giving me Sharing failed toast. What might be the issue ?

Thanks.

Linh
  • 57,942
  • 23
  • 262
  • 279
ZaptechDev Kumar
  • 164
  • 1
  • 14

2 Answers2

2

finally, i got the solution :

 Intent shareIntent = new Intent();
    shareIntent.setAction(Intent.ACTION_SEND);
    shareIntent.setType(getResources().getString(R.string.strIntentType));
    Uri uri = FileProvider.getUriForFile(mContext, getResources().getString(R.string.strFileProviderPackage), mArrayIcons[position]);
    shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
    shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(shareIntent);

Used FileProvider to do so and it works like a charm..!!! yeppii.

ZaptechDev Kumar
  • 164
  • 1
  • 14
  • Hey, one more little issue, Image share with WhatsApp goes with Black background since i am using Transperent png images. i want that it go with white background. Any idea ? – ZaptechDev Kumar Feb 17 '17 at 05:28
1

As your image file location is within your project you can't share image with external app.

Here are few ways to share

  1. create your file provider

  2. store your image file in external directory.

check these links to create your file provider

  1. https://developer.android.com/training/secure-file-sharing/setup-sharing.html

  2. https://developer.android.com/reference/android/support/v4/content/FileProvider.html

  3. http://www.blogc.at/2014/03/23/share-private-files-with-other-apps-fileprovider/

Pratik Popat
  • 2,891
  • 20
  • 31
  • I am using InputMethodService of CommitContent API. – ZaptechDev Kumar Feb 13 '17 at 06:13
  • I can share "text" successfully. But, issue is with image. – ZaptechDev Kumar Feb 13 '17 at 06:16
  • Hello sir, Thanks for your valuable answer. B – ZaptechDev Kumar Feb 14 '17 at 12:54
  • Hello sir, Thanks for your valuable answer. But, I could not share the image with the Facebook Messenger. Why ? Any Idea ? – ZaptechDev Kumar Feb 14 '17 at 13:11
  • One more thing, I am creating virtual keyboard app, my keyboard opens in another messeging app. Now, if that apps does not have support for Commit content API, it will be share via Intent. I am checking that particular app is supporting Commit content API or not. Condition comes true in case of Facebook Messenger. Now, I want to do it with ShareIntent in case of Facebook Messenger.. So, How can i check that Facebook Messenger is currently runing. NOTE : I am using InputMethodService. – ZaptechDev Kumar Feb 14 '17 at 13:22
  • Hey, one more little issue, Image share with WhatsApp goes with Black background since i am using Transperent png images. i want that it go with white background. Any idea ? – ZaptechDev Kumar Feb 17 '17 at 05:28