【问题标题】:Youtube uploading Video AndroidYoutube 上传视频 Android
【发布时间】:2014-12-26 12:53:15
【问题描述】:

我正在尝试将视频上传到 Android 版 YouTube。 我希望用户使用 GoogleAuthUtil 验证他的 Google 帐户,它可以工作。然后我也加载了用户名和令牌以及 uriFile。

我要做的最后一步是将其上传到 Youtube。为此,我遵循了此代码https://code.google.com/p/ytd-android/source/browse/trunk/?r=38

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

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_fanscup.UploadActivity$1.onPostExecute(UploadActivity.java:483)

在 com.library_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.os.Looper.loop(Looper.java:130) 在 android.app.ActivityThread.main(ActivityThread.java:3683)

在 java.lang.reflect.Method.invokeNative(Native Method)

在 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(Native Method)

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

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

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

有人可以帮我吗?有什么想法吗?

【问题讨论】:

    标签: android video upload youtube


    【解决方案1】:

    你可以使用下面的类

    public static void uploadVideo(String msg, final Context context){ //列表范围 = Lists.newArrayList("https://www.googleapis.com/auth/youtube.upload");

        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);
        }
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-30
      • 1970-01-01
      • 1970-01-01
      • 2012-07-19
      • 2015-01-08
      • 2013-02-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多