AndroidアプリケーションでGoogleドライブからファイルをアップロードおよびダウンロードしようとしています。
を使用して、ユーザーのGoogleアカウントの認証トークン文字列を取得できましたAccountManager.getAuthToken()
。
そのトークンを使用してドライブオブジェクトを初期化し、Googleドライブへのリクエストの送信を開始する方法がわかりません。Googleドライブの不足しているドキュメントの例から次のコードを使用しましたが、機能しません。どうやら、呼び出しは成功しましたが、ファイルをアップロードしようとすると、Androidがアプリが応答していないことを通知するまで、電話が切れます。
GoogleCredential credential = new GoogleCredential().setAccessToken(authToken);
HttpTransport httpTransport = new NetHttpTransport();
JsonFactory jsonFactory = new JacksonFactory();
Drive drive = null;
drive = Drive.builder(httpTransport, jsonFactory)
.setApplicationName(APP_NAME)
.setHttpRequestInitializer(credential)
.setJsonHttpRequestInitializer(new GoogleKeyInitializer(API_KEY))
.build();
特定のファイルをアップロードするには、次のようにします。
try
{
// local file
java.io.File fileLocal = new java.io.File(FILE_NAME);
// remote file
File remoteFile = new File();
remoteFile.setTitle(fileLocal.getName());
remoteFile.setDescription("Text File");
remoteFile.setMimeType("text/plain");
// load the content of the local file into the remote content
FileContent remoteFileContent = new FileContent("text/plain", fileLocal);
// upload the file into Google Drive
File resultFile = drive.files().insert(remoteFile, remoteFileContent).execute();
lblStatus.setText("Upload successful with file ID: " + resultFile.getId());
}
catch (Exception e)
{
// error handling
...
}
Google APIドキュメントを読みましたが、それらは混乱していて、クラスには適切なドキュメントがありません。彼らが提供するサンプルはかなり乱雑です。
アプリが応答しない理由は何ですか?私は何が間違っているのですか?どんな助けでも大歓迎です。ありがとう。