Intent + Share + Action_Send_Multiple + Facebook not working

╄→гoц情女王★ 提交于 2019-12-01 08:56:53

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.

Further to the accepted answer (which I have upvoted) here is another method, which also closes the cursor:

private Uri getContentUri(Context context, String contentPath) {
    Cursor c = context.getContentResolver().query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            new String[]{MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATA},
            MediaStore.Images.ImageColumns.DATA + "=?",
            new String[]{contentPath},
            null
    );

    Uri contentUri = null;
    try {
        if (c != null && c.getCount() > 0 && c.moveToFirst()) {
            int colId = c.getColumnIndex(MediaStore.Images.ImageColumns._ID);
            if (colId > -1) {
                int id = c.getInt(colId);
                contentUri = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, Integer.toString(id));
            }
        }
    } catch (Exception e) {
    } finally {
        try {
            if (c != null) {
                c.close();
            }
        } catch (Exception e) {}
    }
    return contentUri;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!