【问题标题】:How to upload video to youtube in android using intent?如何使用意图将视频上传到 android 中的 youtube?
【发布时间】:2015-02-15 15:48:11
【问题描述】:

我正在通过分享按钮将视频上传到 youtube。如果我点击上传按钮,它会显示社交图标,如 whatsapp、facebook、youtube。当我点击 youtube 时,它​​应该被上传到 youtube。

下面是我的代码:

public class MainActivity extends Activity {

private static final int SELECT_FILE1 = 1;
private static final int SELECT_FILE2 = 2;
String selectedPath1 = "NONE";
String selectedPath2 = "NONE";
TextView tv, res;
ProgressDialog progressDialog;
Button b1,b3;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    tv = (TextView)findViewById(R.id.tv);
    res = (TextView)findViewById(R.id.res);
    tv.setText(tv.getText() + selectedPath1);
    b1 = (Button)findViewById(R.id.Button01);

    b3 = (Button)findViewById(R.id.upload);
    b1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            openGallery(SELECT_FILE1);
        }
    });

    b3.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {

             doFileUpload();
        }
    });

}

public void openGallery(int req_code){

    Intent intent = new Intent();
    intent.setType("image/* video/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent,"Select file to upload "), req_code);
  }

  public void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (resultCode == RESULT_OK) 
    {
        Uri selectedImageUri = data.getData();
        if (requestCode == SELECT_FILE1)
        {
            selectedPath1 = getPath(selectedImageUri);
            System.out.println("selectedPath1 : " + selectedPath1);
        }

        tv.setText("Selected File paths : " + selectedPath1 + "," + selectedPath2);
    }
}

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

private void doFileUpload()
{

    //File file1 = new File(selectedPath1);
    Intent intent = new Intent();

    intent.setAction(Intent.ACTION_SEND);
    intent.setType("video/mp4");
    intent.putExtra(Intent.EXTRA_STREAM, selectedPath1);
    startActivity(Intent.createChooser(intent,"Upload video via:"));  

}


}

选择视频后,我可以选择 youtube 图标。然后 youtube 窗口会自动关闭。任何建议都是非常值得赞赏的。谢谢!

【问题讨论】:

    标签: android android-intent video upload youtube


    【解决方案1】:

    试试这个:

    ContentValues content = new ContentValues(4);
    content.put(Video.VideoColumns.DATE_ADDED,
    System.currentTimeMillis() / 1000);
    content.put(Video.Media.MIME_TYPE, "video/mp4");
    content.put(MediaStore.Video.Media.DATA, "video_path");
    ContentResolver resolver = getBaseContext().getContentResolver();
    Uri uri = resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, content);
    
    Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
    sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Title");
    sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM,uri);
    startActivity(Intent.createChooser(sharingIntent,"share:")); 
    

    How to upload video to youtube in android?

    【讨论】:

    • 感谢 Miki 使用此代码时出现错误..请查找错误日志。
    • 你应该添加这个权限:android.permission.WRITE_EXTERNAL_STORAGE
    • 谢谢三木,我在清单中添加了权限,现在我得到了像没有应用程序可以执行此操作的权限
    • 很好的答案@MikiFranko。但是我可以使用意图在 youtube 上传页面中填写标题和描述吗?
    • 我只收到“没有应用程序可以执行此操作”,为什么?
    【解决方案2】:
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_SEND);
        intent.setType("video/mp4");
        intent.putExtra(android.content.Intent.EXTRA_SUBJECT,"Title_text");
        intent.putExtra(Intent.EXTRA_STREAM,selectedImageUri);
        startActivity(Intent.createChooser(intent,"Upload video via:"));
    

    【讨论】:

      猜你喜欢
      • 2013-04-27
      • 2013-04-04
      • 2011-06-10
      • 1970-01-01
      • 2011-06-13
      • 2011-11-30
      • 2013-09-07
      • 2013-05-30
      • 1970-01-01
      相关资源
      最近更新 更多