从安卓应用程序分享视频



Ours 是一个视频托管门户,用户可以根据他们获得的观看次数上传他们的视频并从中获利。我们最近推出了一个Android应用程序,并尝试将"共享"按钮集成到每个视频中。这是我们放置的代码

 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();
                    }

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

我们该怎么做呢?

这对

我有用。试一试!

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();

您应该先下载视频。然后,您可以使用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");

我想,所有其他解决方案都已经过时了。这是将视频共享到任何平台(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
}

要在 android 10 及更高版本中发送视频,请使用这个。我用它来将视频从本地存储发送到WhatsApp。

    public void shareVideo(String filepath)
    {
    
     Intent shareintent=new Intent("android.intent.action.SEND");      
     shareintent.setType("video/mp4");       
     shareintent.putExtra("android.intent.extra.STREAM", 
     Uri.parse(filepath));
     startActivity(Intent.createChooser(shareintent,"share"));
    }

最新更新