从Android应用分享视频

时间:2017-04-05 07:12:17

标签: android video share

我们是一个视频托管门户网站,用户可以根据他们获得的观看次数上传视频并从中获取。我们最近推出了一款Android App,并试图将Share按钮集成到每个视频中。这是我们放置的代码

 Intent intent = new Intent();
                    try {

                        URL url = new URL("https://www.clipsnow.com/videos/images/thumbnails/230/10493.jpg");
                        Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
                        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                        intent.setAction(Intent.ACTION_SEND);
                        intent.setData(Uri.parse("https://www.clipsnow.com"));

                        intent.putExtra(Intent.EXTRA_TEXT,msg);

                        intent.setType("text/plain");
                        intent.putExtra(Intent.EXTRA_STREAM, getImageUri(v.getContext(), image));


                        intent.setType("image/*");
                        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
                        v.getContext().startActivity(Intent.createChooser(intent, "Share Video"));

                    } catch (Exception e) {
                        e.printStackTrace();
                    }

当我们与此分享任何视频时,只有缩略图与视频标题一起共享。但是,我们需要共享视频网址,当用户点击该网址时,用户将被带到我们的应用中。

我们怎么能这样做?

3 个答案:

答案 0 :(得分:3)

这对我有用。试一试!

File videoFile = new File(filePath);
Uri videoURI = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
        ? FileProvider.getUriForFile(mContext, mContext.getPackageName(), videoFile)
        : Uri.fromFile(videoFile);
ShareCompat.IntentBuilder.from(getActivity())
        .setStream(videoURI)
        .setType("video/mp4")
        .setChooserTitle("Share video...")
        .startChooser();

答案 1 :(得分:1)

我想,所有其他解决方案都已过时。这是将视频共享到任何平台(Youtube,Gmail,环聊,Whatsapp等)的有效解决方案,

startActivity(
    Intent.createChooser(
        Intent().setAction(Intent.ACTION_SEND)
            .setType("video/*")
            .setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
            .putExtra(
                Intent.EXTRA_STREAM,
                getVideoContentUri(this, File(currentVideo.videoPath))
            ), resources.getString(R.string.share_video)
    )
)

下面是getVideoContentUri方法,

/**
 * Return the URI for a file. This URI is used for
 * sharing of video.
 * NOTE: You cannot share a file by file path.
 *
 * @param context Context
 * @param videoFile File
 * @return Uri?
 */
fun getVideoContentUri(context: Context, videoFile: File): Uri? {
    var uri: Uri? = null
    val filePath = videoFile.absolutePath
    val cursor = context.contentResolver.query(
        MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
        arrayOf(MediaStore.Video.Media._ID),
        MediaStore.Video.Media.DATA + "=? ",
        arrayOf(filePath), null)

    if (cursor != null && cursor.moveToFirst()) {
        val id = cursor.getInt(cursor
            .getColumnIndex(MediaStore.MediaColumns._ID))
        val baseUri = Uri.parse("content://media/external/video/media")
        uri = Uri.withAppendedPath(baseUri, "" + id)
    } else if (videoFile.exists()) {
        val values = ContentValues()
        values.put(MediaStore.Video.Media.DATA, filePath)
        uri = context.contentResolver.insert(
            MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values)
    }

    closeCursor(cursor)
    return uri
}

答案 2 :(得分:0)

您应首先下载视频。然后,您可以使用ACTION_SEND进行分享。

        String path = ""; //should be local path of downloaded video

        ContentValues content = new ContentValues(4);
        content.put(MediaStore.Video.VideoColumns.DATE_ADDED,
                System.currentTimeMillis() / 1000);
        content.put(MediaStore.Video.Media.MIME_TYPE, "video/mp4");
        content.put(MediaStore.Video.Media.DATA, path);

        ContentResolver resolver = getApplicationContext().getContentResolver();
        Uri uri = resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, content);

        Intent sharingIntent = new Intent(Intent.ACTION_SEND);
        sharingIntent.setType("video/*");
        sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Hey this is the video subject");
        sharingIntent.putExtra(Intent.EXTRA_TEXT, "Hey this is the video text");
        sharingIntent.putExtra(Intent.EXTRA_STREAM,uri);
        startActivity(Intent.createChooser(sharingIntent,"Share Video");