1

重複の可能性:
Androidを開いてGoogleドライブSDKとの間でファイルを保存する

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ドキュメントを読みましたが、それらは混乱していて、クラスには適切なドキュメントがありません。彼らが提供するサンプルはかなり乱雑です。

アプリが応答しない理由は何ですか?私は何が間違っているのですか?どんな助けでも大歓迎です。ありがとう。

4

1 に答える 1

0

私は数週間前にこの問題を抱えていました。こちらをご覧ください。ここでは、Google ドライブを Android 上で動作させる方法について、かなり詳細な説明を書いています。(主に、ファイルのダウンロードが適切に許可されていないように見えるためです...)

Android Google ドライブ SDK との間でファイルを開いたり保存したりします

于 2012-09-19T18:42:28.807 に答える