如何使用android youtube api上传视频?

时间:2016-04-06 09:28:34

标签: android youtube-api youtube-data-api

我正在尝试使用youtube创建一个andoid应用程序。 我想使用网址上传视频。 使用我的代码,我收到了200个http状态代码和成功消息,但实际上并没有。 我该如何解决?

URL url = new URL(urlString);
            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
            conn.setDoInput(true);
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setRequestMethod("PUT");
            conn.setRequestProperty("Authorization", String.format("Bearer %s", access_token));
            conn.setRequestProperty("Content-Type", "video/*");
            conn.setRequestProperty("Content-Length", ContentLength);

            File file = new File(path);

            FileInputStream fis = new FileInputStream(file);
            DataOutputStream dos = new DataOutputStream(conn.getOutputStream());

            int numberBytes = fis.available();
            byte bytearray[] = new byte[numberBytes];
            Log.e(" FileLength", String.valueOf(bytearray.length));
            for(int i = 0; i < bytearray.length; i++)
                dos.write(bytearray[i]);
            dos.flush();

            fis.close();
            dos.close();

            int responseCode = conn.getResponseCode();
            if(responseCode == 200) {
                Log.e("ResponseCode", String.valueOf(responseCode));

                InputStream is = conn.getInputStream();
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                byte[] byteBuffer = new byte[1024];
                byte[] byteData = null;
                int nLength = 0;
                while((nLength = is.read(byteBuffer, 0, byteBuffer.length)) != -1) {
                    baos.write(byteBuffer, 0, nLength);
                }
                byteData = baos.toByteArray();

                String response = new String(byteData);

                Log.e("RESPONSE", response);
            }

        } catch(Exception e) {
            e.printStackTrace();
        }

1 个答案:

答案 0 :(得分:0)

使用此library project

该代码是Android操作系统应用程序的参考实现,可捕获视频,上传到YouTube,

详细答案:uploading using above library project