【问题标题】:Share Video From Android App从 Android 应用分享视频
【发布时间】:2017-08-30 15:48:59
【问题描述】:

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 时,用户将被带到我们的应用程序。

我们该怎么做?

【问题讨论】:

    标签: android video share


    【解决方案1】:

    我想,所有其他解决方案都已过时。这是将视频分享到任何平台(Youtube、Gmail、Hangout、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】:

    这对我有用。试试看!

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

    【讨论】:

    • 它不适用于奥利奥。视频文件被共享,但是当我们启动应用程序以验证它时,当我们尝试以“无法加载资源”启动视频时显示错误
    • @suv 我认为现在应该。
    • 找不到具有权限 package_name 的提供者的元数据
    【解决方案3】:

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

    【讨论】:

    • 我们不希望用户将视频下载到本地存储。如果我们通过 WhatsApp 分享来自 YouTube 的任何视频,它将向接收者分享缩略图、视频标题和视频 URL。我们希望对我们的应用执行相同的实现。
    • @aswarth 这是不同的东西。您应该创建帖子/视频详细信息页面并为其设置 html 标签。请检查stackoverflow.com/a/35785393/1923925 这个stackoverflow.com/questions/19778620/…
    • @aswarth Youtube 分享给 whatsapp 的只是一个链接,然后 Whatsapp 获取 Url 链接以获取图像和标题,如果有的话,还有描述。
    猜你喜欢
    • 1970-01-01
    • 2013-02-05
    • 1970-01-01
    • 2012-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-23
    • 1970-01-01
    相关资源
    最近更新 更多