美文网首页Android iOS开发知识库
分享视频到YouTube遇到的坑

分享视频到YouTube遇到的坑

作者: 进击的鱼儿 | 来源:发表于2018-04-27 17:55 被阅读144次

    1、一般的分享视频到指定第三方平台代码

    private void shareToThird(Activity activity, String packageName, String mediaPath) {
    
            // Create the new Intent using the 'Send' action.
            Intent share = new Intent(Intent.ACTION_SEND);
            // Set the MIME type
            share.setType("video/*");
            Uri    uri = Uri.fromFile(new File(mediaPath));
            share.putExtra(android.content.Intent.EXTRA_STREAM, uri);
            activity.startActivity(Intent.createChooser(share, "share to"));
        }
    

    2、分享到指定的应用

    要分享指定的应用首先需要知道应用的包名和对应的分享页面的Activity名,可以通过以下方式获取

    Intent share = new Intent(Intent.ACTION_SEND);
            // Set the MIME type
            share.setType(type);//视频type = "video/*",图片"image/*"
            PackageManager packageManager = VLCApplication.getAppContext().getPackageManager();
            List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(share,       PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
            ResolveInfo resolveInfo = null;
            for (ResolveInfo info : resolveInfos) {
                Log.e(TAG, "" + info.activityInfo.packageName + "---" + info.activityInfo.name);
            }
    

    所以分享到指定应用的代码为

    private void shareToThird(Activity activity, String packageName, String mediaPath) {
    // Create the new Intent using the 'Send' action.
            Intent share = new Intent(Intent.ACTION_SEND);
            // Set the MIME type
            share.setType(type);
            PackageManager packageManager = VLCApplication.getAppContext().getPackageManager();
            List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(share, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
            ResolveInfo resolveInfo = null;
            for (ResolveInfo info : resolveInfos) {
                Log.e(TAG, "" + info.activityInfo.packageName + "---" + info.activityInfo.name);
                if (packageName.equals(info.activityInfo.packageName)) {
                    resolveInfo = info;
                    break;
                }
            }
            Uri uri = Uri.fromFile(new File(mediaPath));
            share.putExtra(android.content.Intent.EXTRA_STREAM, uri);
            share.setClassName(packageName, resolveInfo.activityInfo.name);//注意这里Activity名不能写死,因为有些app升级后分享页面的路径或者名称会更改(比如Instagram)
            activity.startActivity(Intent.createChooser(share, "share to"));
        }
    

    这里要注意share.setClassName(String packageName,String activityName),其中activityName不能写死,因为有可能会变。这就是为什么每次分享的时候需要重新查询一遍支持分享的应用的原因。这段代码用在大部分的应用分享都没有问题,直到遇到分享YouTube!分享到YouTube的视频总是无法解析出来,几经周折发现是uri的问题,分享到YouTube的uri需要是媒体库里面的uri!

    最终代码如下:

    private void shareToThird(Activity activity, String type, String packageName, String mediaPath) {
    
            // Create the new Intent using the 'Send' action.
            Intent share = new Intent(Intent.ACTION_SEND);
            // Set the MIME type
            share.setType(type);
            PackageManager packageManager = VLCApplication.getAppContext().getPackageManager();
            List<ResolveInfo> resolveInfos = packageManager.queryIntentActivities(share, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);
            ResolveInfo resolveInfo = null;
            for (ResolveInfo info : resolveInfos) {
                Log.e(TAG, "" + info.activityInfo.packageName + "---" + info.activityInfo.name);
                if (packageName.equals(info.activityInfo.packageName)) {
                    resolveInfo = info;
                    break;
                }
            }
            if (resolveInfo == null) {
                Uri uri = Uri.parse("market://details?id=" + packageName);
                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                activity.startActivity(intent);
                return;
            }
            Uri uri;
            if ("video/*".equals(type)) {
                uri = queryUriForVideo(activity, new File(mediaPath));
                if (uri == null) {
                    uri = insertVideo(mediaPath);
                }
            } else {
                uri = Uri.fromFile(new File(mediaPath));
            }
            share.putExtra(android.content.Intent.EXTRA_STREAM, uri);
            share.setClassName(packageName, resolveInfo.activityInfo.name);
            activity.startActivity(Intent.createChooser(share, "share to"));
        }
    
      /**
        * 插入视频到MediaStore
        */
        private Uri insertVideo(String mediaPath) {
            ContentValues content = new ContentValues(4);
            content.put(MediaStore.Video.VideoColumns.DATE_ADDED, System.currentTimeMillis() / 1000);
            content.put(MediaStore.Video.Media.MIME_TYPE, "video/*");
            content.put(MediaStore.Video.Media.DATA, mediaPath);
            ContentResolver resolver = VLCApplication.getAppContext().getContentResolver();
            return resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, content);
        }
    
        /**
         * 查找视频文件对应于MediaStore的Uri
         *
         * @param file 视频文件
         * @return
         */
    
        private Uri queryUriForVideo(Activity activity, File file) {
            int id = getId(activity, file);
            if (id == -1) {
                return null;
            }
            return Uri.withAppendedPath(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, String.valueOf(id));
        }
    
    
        private int getId(Activity activity, File f) {
            int id = -1;
            String[] mediaColumns = {MediaStore.Video.Media._ID,
                    MediaStore.Video.Media.DATA, MediaStore.Video.Media.TITLE,
                    MediaStore.Video.Media.MIME_TYPE,
                    MediaStore.Video.Media.DISPLAY_NAME};
            final String where = MediaStore.Video.Media.DATA + "=" + "?";
            Cursor cursor = activity.managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                    mediaColumns, where, new String[]{f.getAbsolutePath()}, null);
            if (cursor == null) {
                return -1;
            }
            if (cursor.moveToFirst()) {
                do {
                    id = cursor.getInt(cursor
                            .getColumnIndex(MediaStore.Video.Media._ID));
                } while (cursor.moveToNext());
            }
            return id;
        }
    

    上述代码对所有应用都有效,没有对图片的uri做同样的处理,因为YouTube不能分享图片,偷一波懒~~

    相关文章

      网友评论

        本文标题:分享视频到YouTube遇到的坑

        本文链接:https://www.haomeiwen.com/subject/efrilftx.html