Image Share Intent Works for Gmail But Crashes Fb and Twitter

Image share intent works for Gmail but crashes FB and twitter

EDIT -- The workaround described in this answer works, but the solution provided by Stefan is more elegant. It should be the accepted answer.

Also, as of January 2015, the Facebook app no longer has this bug (and now works with the standard FileProvider) but Twitter still does. Hopefully it will go completely away in the future, and neither of these will be necessary.


While the FileProvider object looks wonderful in theory, it doesn't seem to work when sharing to many third-party applications (while Google ones, like G+, Gmail, &c work perfectly). Either it doesn't expose sufficient information, or those applications just assume you're sharing files, and thus crash.

It's really frustrating! :/

The only reliable way I've found to share image files is to copy them to the external storage directory (you can create a .nomedia subdirectory to avoid them appearing in the Gallery app).

For example:

Bitmap bitmapToShare = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);

File pictureStorage = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File noMedia = new File(pictureStorage, ".nomedia");
if (!noMedia.exists())
noMedia.mkdirs();

File file = new File(noMedia, "shared_image.png");
saveBitmapAsFile(bitmapToShare, file);

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
shareIntent.setType("image/png");

startActivity(shareIntent);

Addendum: of course, you should also check getExternalStorageState() before trying to write/copy the file there.

Intent + Share + Action_Send_Multiple + Facebook not working

After trying lots of things Finally I came to solution that In current version Facebook broke Intent for accepting Multiple Images while coming as Intent. ACTION_SEND_MULTIPLE. But I surprised when Multiple sharing is working with default Gallery in my device.
Finally with the help of my friend (not wanted to take credit from him). I came to know that Default GAllery is sending files in URI by using Content Provider thats why they are working So I have changed my URI list for Facebook (I have created custom share dialog, So I can intercept which option is selected from Share Dialog opened via Intent.)

private void updateUrisForFacebook() {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_SEND_MULTIPLE)) {
ArrayList<Uri> uris = intent
.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
for (int i = 0; i < uris.size(); i++) {
Uri uri = uris.get(i);
String path = uri.getPath();
File imageFile = new File(path);
uri = getImageContentUri(imageFile);
uris.set(i, uri);
}
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
}
}

private Uri getImageContentUri(File imageFile) {
String filePath = imageFile.getAbsolutePath();
Cursor cursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Images.Media._ID },
MediaStore.Images.Media.DATA + "=? ",
new String[] { filePath }, null);
if (cursor != null && cursor.moveToFirst()) {
int id = cursor.getInt(cursor
.getColumnIndex(MediaStore.MediaColumns._ID));
Uri baseUri = Uri.parse("content://media/external/images/media");
return Uri.withAppendedPath(baseUri, "" + id);
} else {
if (imageFile.exists()) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, filePath);
return context.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
} else {
return null;
}
}
}

Finally It works :)

So Basic Idea is to convert your URI list into Content provider URI list and then check it will work.

Cannot share images from internal storage despite using a ContentProvider

Turns out there are missing permissions when some package wants to access my application's internal files. I wasn't able to figure out how to properly grant these permissions, albeit trying everything I found in the many SO posts on this topic. Switching to external storage instead of the internal one solved this.

Android gmail doesn't appear in mail intent share list on nexus device(Tablet and phone)

The problem here is that you are using

mailIntent.putExtra(Intent.EXTRA_STREAM, uris);

This was just a bug in an old version of the gMail-app. The app wasn't registered to the intent's action and so it didn't appear in the suggested apps list.

By now this bug has been fixed and it works again.



Related Topics



Leave a reply



Submit