Reputation: 164
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
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
create your file provider
store your image file in external directory.
check these links to create your file provider
https://developer.android.com/training/secure-file-sharing/setup-sharing.html
https://developer.android.com/reference/android/support/v4/content/FileProvider.html
http://www.blogc.at/2014/03/23/share-private-files-with-other-apps-fileprovider/
Upvotes: 1
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