【问题标题】:unable to upload video through action_send intent in android 4.3无法通过 android 4.3 中的 action_send 意图上传视频
【发布时间】:2014-01-24 13:54:43
【问题描述】:

我可以通过此代码上传视频,直到 4.1.2 版本的 android。但在 4.3 中失败了。

调用shareVideo()方法android会显示一个应用列表,但是在选择的情况下 youtube 从列表中,youtube 应用程序打开然后停止,没有任何消息,如果选择 instagram,instagram 应用程序崩溃。我也无法通过包括 facebook 在内的任何应用上传视频。

请告诉我是什么问题??

提前致谢。

public void shareVideo(View view){
    new Handler().post(new Runnable() {

        @Override
        public void run() {
            // TODO Auto-generated method stub
            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, filename);

            Uri videoURI = getBaseContext().getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, content);
             //Uri videoURI = Uri.fromFile(new File(filename));
                Intent intent = new Intent(Intent.ACTION_SEND);
                //intent.setAction(Intent.ACTION_SEND);
                intent.setType("video/mp4");
                intent.putExtra(MediaStore.EXTRA_OUTPUT, videoURI);
                try {
                startActivity(Intent.createChooser(intent,"Upload video via:"));
                } catch (android.content.ActivityNotFoundException ex) {

                }
        }
    });

    }

【问题讨论】:

    标签: android video android-intent file-upload youtube


    【解决方案1】:

    android 4.1 之后改变了sd 卡路径的机制。现在我可以通过视频文件的 id 获取 uri 来上传视频。 这是我的代码

    public static String getVideoContentUriFromFilePath(Context ctx, String filePath) {
    
          ContentResolver contentResolver = ctx.getContentResolver();
          String videoUriStr = null;
             long videoId = -1;
             Log.d("first log","Loading file " + filePath);
    
             // This returns us content://media/external/videos/media (or something like that)
             // I pass in "external" because that's the MediaStore's name for the external
             // storage on my device (the other possibility is "internal")
             Uri videosUri = MediaStore.Video.Media.getContentUri("external");
    
             Log.d("second log","videosUri = " + videosUri.toString());
    
             String[] projection = {MediaStore.Video.VideoColumns._ID};
    
             // TODO This will break if we have no matching item in the MediaStore.
             Cursor cursor = contentResolver.query(videosUri, projection, MediaStore.Video.VideoColumns.DATA + " LIKE ?", new String[] { filePath }, null);
             cursor.moveToFirst();
    
             int columnIndex = cursor.getColumnIndex(projection[0]);
             videoId = cursor.getLong(columnIndex);
    
             Log.d("third log","Video ID is " + videoId);
             cursor.close();
             if (videoId != -1 ) videoUriStr = videosUri.toString() + "/" + videoId;
             return videoUriStr;
         }
    

    点击上传按钮 使用这个方法-

    public void shareVideo(View view){
        new Handler().post(new Runnable() {
    
            @Override
            public void run() {
    String newPath=getVideoContentUriFromFilePath(ShareVideoActivity.this, videoPath);
    Intent intent = new Intent(Intent.ACTION_SEND);
                         intent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Title");
                    //intent.setAction(Intent.ACTION_SEND);
                    intent.setType("video/mp4");
                    intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(newPath));
                    try {
                    startActivity(Intent.createChooser(intent,"Upload video via:"));
                    } catch (android.content.ActivityNotFoundException ex) {
    
                    }
        }
            }
        });
    
    }
    

    【讨论】:

    • 我正在测试 KitKat android,只有这段代码对我有用,非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-30
    • 1970-01-01
    • 2015-06-11
    • 1970-01-01
    相关资源
    最近更新 更多