Share video to viber via intents in android not working

699 views Asked by At

I googled a lot but no success. I want to share a video file in my App via this code:

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("video/mp4"); //or even video mpeg not working!
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + outputFileInformation.getFullPath()));
startActivity(Intent.createChooser(intent, getString(R.string.share)));

And I tried some other codes, But for in sharing list if I choose Viber, After selecting recipient nothing happens, Screen flashes and nothing happens. (I must say sharing image/png has no issue. I really need to get this work soon. I could easily share that video from gallery without any issue but now working in my app... .

1

There are 1 answers

0
user1737875 On BEST ANSWER

I manged to make it work. I must make an Uri in another way, here is the code i used for making Uri from a video file path (you could change it for Images as well)

public static Uri getVideoContentUri(Context context, File imageFile) {
    String filePath = imageFile.getAbsolutePath();
    Cursor cursor = context.getContentResolver().query(
            MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
            new String[] { MediaStore.Video.Media._ID },
            MediaStore.Video.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/video/media");
        return Uri.withAppendedPath(baseUri, "" + id);
    } else {
        if (imageFile.exists()) {
            ContentValues values = new ContentValues();
            values.put(MediaStore.Video.Media.DATA, filePath);
            return context.getContentResolver().insert(
                    MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
        } else {
            return null;
        }
    }
}