我尝试使用以下代码上传视频。但是每当我点击上传时,它都会进入INITIATION_STARTED和INITIATION_COMPLETE的ResumableUpload类。但它没有上传到youtube。我找不到任何异常,我也无法理解为什么它没有进入MEDIA_IN_PROGRESS。
你能帮我解决..
获取访问令牌的代码,并从MainActivity调用uploadservice
MainActivity.java:
AND (bcp.value is null or bcp.value = cm.id)
UploadService.java
公共类UploadService扩展了IntentService {
private void tryAuthenticate() {
if (isFinishing()) {
return;
}
mToken = null;
setProgressBarIndeterminateVisibility(true);
AsyncTask<Void, Void, Boolean> task = new AsyncTask<Void, Void, Boolean>() {
@Override
protected Boolean doInBackground(Void... params) {
try {
// Retrieve a token for the given account and scope. It will
// always return either
// a non-empty String or throw an exception.
mToken =
GoogleAuthUtil.getToken(MainActivity.this, mChosenAccountName, "oauth2:"
+ Scopes.PROFILE + " " + YouTubeScopes.YOUTUBE + " "
+ YouTubeScopes.YOUTUBE_UPLOAD);
} catch (GooglePlayServicesAvailabilityException playEx) {
GooglePlayServicesUtil.getErrorDialog(playEx.getConnectionStatusCode(),
MainActivity.this, REQUEST_GMS_ERROR_DIALOG).show();
} catch (UserRecoverableAuthException userAuthEx) {
// Start the user recoverable action using the intent
// returned by
// getIntent()
startActivityForResult(userAuthEx.getIntent(), REQUEST_AUTHENTICATE);
return false;
} catch (IOException transientEx) {
// TODO: backoff
Log.e(this.getClass().getSimpleName(), transientEx.getMessage());
} catch (GoogleAuthException authEx) {
Log.e(this.getClass().getSimpleName(), authEx.getMessage());
}
return true;
}
@Override
protected void onPostExecute(Boolean hideProgressBar) {
invalidateOptionsMenu();
if (hideProgressBar) {
setProgressBarIndeterminateVisibility(false);
}
if (mToken != null) {
runOnUiThread(new Runnable() {
@Override
public void run() {
saveAccount();
}
});
}
loadData();
}
};
task.execute((Void) null);
}
public void uploadVideo(View view) {
if (mToken == null) {
return;
}
// if an upload video is selected.
if (mVideoData != null) {
mDirectFragment.directLite(mVideoData, mToken);
mButton.setEnabled(false);
return;
}
// if a video is picked or recorded.
if (mFileURI != null) {
Intent uploadIntent = new Intent(this, UploadService.class);
uploadIntent.setData(mFileURI);
uploadIntent.putExtra(ACCOUNT_KEY, mChosenAccountName);
uploadIntent.putExtra(TOKEN_KEY, mToken);
startService(uploadIntent);
mButton.setEnabled(false);
}
}
}
ResumableUpload.java:
public class ResumableUpload {
public UploadService() {
super("YTUploadService");
}
private Uri mFileUri;
private String mToken;
private long mFileSize;
@Override
protected void onHandleIntent(Intent intent) {
mFileUri = intent.getData();
Log.d("upload service"," "+mFileUri);
mToken = intent.getStringExtra("token");
mFileSize = intent.getLongExtra("length", 0);
GoogleCredential credential = new GoogleCredential();
credential.setAccessToken(mToken);
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
YouTube youtube =
new YouTube.Builder(httpTransport, jsonFactory, credential).setApplicationName(
Constants.APP_NAME).build();
InputStream fileInputStream = null;
try {
mFileSize = getContentResolver().openFileDescriptor(mFileUri, "r").getStatSize();
fileInputStream = getContentResolver().openInputStream(mFileUri);
} catch (FileNotFoundException e) {
Log.e(getApplicationContext().toString(), e.getMessage());
}
ResumableUpload.upload(youtube, fileInputStream, mFileSize, getApplicationContext());
}
}