Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Youtube上传视频Android_Android_Video_Upload_Youtube - Fatal编程技术网

Youtube上传视频Android

Youtube上传视频Android,android,video,upload,youtube,Android,Video,Upload,Youtube,我正在尝试用Android将视频上传到YouTube。 我希望用户使用GoogleAuthUtil使用他的Google帐户进行验证,并且它可以正常工作。然后,我有用户名和令牌,并加载了URI文件 我想做的最后一步是把它上传到Youtube上。为此,我遵循以下代码 我的代码中的问题在这一部分: File file = new File(fileUri.getPath()); long mFileSize = file.length(); GoogleCredential credential =

我正在尝试用Android将视频上传到YouTube。 我希望用户使用GoogleAuthUtil使用他的Google帐户进行验证,并且它可以正常工作。然后,我有用户名和令牌,并加载了URI文件

我想做的最后一步是把它上传到Youtube上。为此,我遵循以下代码

我的代码中的问题在这一部分:

File file = new File(fileUri.getPath());
long mFileSize = file.length();
GoogleCredential credential = new GoogleCredential();
credential.setAccessToken(mToken);

HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();

YouTube youtube =
        new YouTube.Builder(httpTransport, jsonFactory, credential).setApplicationName(
                "fanscup").build();

InputStream fileInputStream = null;
try {
    mFileSize = getContentResolver().openFileDescriptor(fileUri, "r").getStatSize();
    fileInputStream = getContentResolver().openInputStream(fileUri);
} catch (FileNotFoundException e) {
    Log.e(getApplicationContext().toString(), e.getMessage());
}
ResumableUpload.upload(youtube, fileInputStream, mFileSize, getApplicationContext());
当我尝试上传视频时,我遇到了以下错误:

未捕获处理程序:由于未捕获异常而退出主线程

java.io.FileNotFoundException:/dev/kmsg(权限被拒绝)

java.io.IOException:权限被拒绝

将JitTable从4096调整为8192

致命异常:主

java.lang.NoClassDefFoundError:com.google.api.client.util.Clock

位于com.google.api.client.auth.oauth2.Credential。(Credential.java:200)

在com.google.api.client.googleapis.auth.oauth2.GoogleCredential上。(GoogleCredential.java:187)

在com.library\u fanscup.UploadActivity$1.onPostExecute(UploadActivity.java:483)

在com.library\u fanscup.UploadActivity$1.onPostExecute(UploadActivity.java:1)

位于android.os.AsyncTask.finish(AsyncTask.java:417)

在android.os.AsyncTask.access$300(AsyncTask.java:127)

位于android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)

位于android.os.Handler.dispatchMessage(Handler.java:99)

位于android.app.ActivityThread.main(ActivityThread.java:3683)的android.os.Looper.loop(Looper.java:130)

位于java.lang.reflect.Method.Invokenactive(本机方法)

位于java.lang.reflect.Method.invoke(Method.java:507)

在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:895)上

位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:653)

在dalvik.system.NativeStart.main(本机方法)

异常:java.io.FileNotFoundException:/dev/kmsg(权限被拒绝)

异常:java.io.FileNotFoundException:/dev/kmsg(权限被拒绝)

java.io.FileNotFoundException:/data/plog.log(权限被拒绝)

有人能帮我吗?有什么想法吗

你可以在课下使用

公共静态void上传视频(字符串msg,最终上下文){ //列表范围=Lists.newArrayList(“”)

    HttpTransport transport = AndroidHttp.newCompatibleTransport();
    JsonFactory jsonFactory = new GsonFactory();
    GoogleCredential googleCredential =
            new GoogleCredential.Builder()
                    .setTransport(transport)
                    .setJsonFactory(jsonFactory)
                    .setClientSecrets("client_id", "client_scretekey").build();
    googleCredential.setAccessToken("Access_token");
    googleCredential.setRefreshToken("refresh_token");

    String appName ="";
    final YouTube youtube =
            new YouTube.Builder(transport, jsonFactory, googleCredential).setApplicationName(
                    appName).build();

    InputStream fileInputStream = null;
    long fileSize = 0;
    File filem=new File(msg);
    Uri mFileUri=Uri.fromFile(filem);
    try {
        fileSize = context.getContentResolver().openFileDescriptor(mFileUri, "r").getStatSize();
        fileInputStream = context.getContentResolver().openInputStream(mFileUri);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    final NotificationManager notifyManager =
            (NotificationManager)context. getSystemService(Context.NOTIFICATION_SERVICE);
    final NotificationCompat.Builder builder = new NotificationCompat.Builder(context);
    builder.setContentTitle("Uploading Video")
            .setContentText("Live Streaming")
            .setSmallIcon(R.mipmap.ic_launcher)
            .build();

    String videoId = null;
    try {
        // Add extra information to the video before uploading.
        Video videoObjectDefiningMetadata = new Video();
        VideoStatus status = new VideoStatus();
        status.setPrivacyStatus("public");

        videoObjectDefiningMetadata.setStatus(status);

        // We set a majority of the metadata with the VideoSnippet object.
        VideoSnippet snippet = new VideoSnippet();

        Calendar cal = Calendar.getInstance();
        snippet.setTitle("Live Streaming Upload On " + cal.getTime());
        snippet.setDescription("Live Streaming Upload video on youtube "
                + "on " + cal.getTime());
        // Set your keywords.
        snippet.setTags(Arrays.asList("Android client 1",
                Upload.generateKeywordFromPlaylistId("client_id")));
        // Set completed snippet to the video object.
        videoObjectDefiningMetadata.setSnippet(snippet);
        InputStreamContent mediaContent =
                new InputStreamContent(VIDEO_FILE_FORMAT, new BufferedInputStream(fileInputStream));
        mediaContent.setLength(fileSize);
        YouTube.Videos.Insert videoInsert =
                youtube.videos().insert("snippet,statistics,status", videoObjectDefiningMetadata,
                        mediaContent);


                // Set the upload type and add event listener.
                MediaHttpUploader uploader = videoInsert.getMediaHttpUploader();

                uploader.setDirectUploadEnabled(false);

                final long FileSize = fileSize;
                MediaHttpUploaderProgressListener progressListener = new MediaHttpUploaderProgressListener() {
                    public void progressChanged(MediaHttpUploader uploader) throws IOException {
                        switch (uploader.getUploadState()) {
                            case INITIATION_STARTED:
                                builder.setContentText(context.getString(R.string.initiation_started)).setProgress((int) FileSize,
                                        (int) uploader.getNumBytesUploaded(), false);

                                notifyManager.notify((int)uniqueId, builder.build());
                                break;
                            case INITIATION_COMPLETE:
                                builder.setContentText(context.getString(R.string.initiation_completed)).setProgress((int) FileSize,
                                        (int) uploader.getNumBytesUploaded(), false);
                                notifyManager.notify((int)uniqueId, builder.build());
                                break;
                            case MEDIA_IN_PROGRESS:
                                builder
                                        .setContentTitle(context.getString(R.string.youtube_upload) +
                                                (int) (uploader.getProgress() * 100) + "%")
                                        .setContentText(context.getString(R.string.upload_in_progress))
                                        .setProgress((int) FileSize, (int) uploader.getNumBytesUploaded(), false);
                                notifyManager.notify((int)uniqueId, builder.build());
                                break;
                            case MEDIA_COMPLETE:
                                builder.setContentTitle(context.getString(R.string.yt_upload_completed))
                                        .setContentText(context.getString(R.string.upload_completed))
                                        // Removes the progress bar
                                        .setProgress(0, 0, false);
                                notifyManager.notify((int)uniqueId, builder.build());

                            case NOT_STARTED:
                                Log.d(this.getClass().getSimpleName(),context.getString(R.string.upload_not_started));
                                break;
                        }
                    }
                };
                uploader.setProgressListener(progressListener);

        // Execute upload.
        Video returnedVideo = videoInsert.execute();
        Log.d(TAG, "Video upload completed");

        videoId = returnedVideo.getId();
        Log.d(TAG, String.format("videoId = [%s]", videoId));
        //delete video after video upload completed

    } catch (final GooglePlayServicesAvailabilityIOException availabilityException) {
        Log.e(TAG, "GooglePlayServicesAvailabilityIOException", availabilityException);
        notifyFailedUpload(context, context.getString(R.string.cant_access_play), notifyManager, builder);
    } catch (UserRecoverableAuthIOException userRecoverableException) {
        Log.i(TAG, String.format("UserRecoverableAuthIOException: %s",
                userRecoverableException.getMessage()));
        requestAuth(context, userRecoverableException);
    } catch (IOException e) {
        Log.e(TAG, "IOException", e);
        notifyFailedUpload(context,context.getString(R.string.please_try_again), notifyManager, builder);
    }

}