ZaptechDev Kumar
ZaptechDev Kumar

Reputation: 164

Sharing Failed in Intent

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.

Upvotes: 0

Views: 2487

Answers (2)

Pratik Popat
Pratik Popat

Reputation: 2999

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/

Upvotes: 1

ZaptechDev Kumar
ZaptechDev Kumar

Reputation: 164

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.

Upvotes: 2

Related Questions