2016-10-11 10 views
0

Googleのクイックスタートガイドに従って画像を撮影し、その後ユーザーのGoogleドライブにアップロードします。Android StudioとGoogleドライブAPI(Googleドライブに動画をアップロード)

私の質問は、ビデオを使用してビデオをアップロードし、画像ではなくGoogleドライブにビデオをアップロードするにはどうすればよいですか?私はすでにビデオの使用方法にアクセスして録画する方法を知っています(それを外部ストレージに保存する)が、後でアップロードする方法はありません。私の推測では、次のコードとほとんど同じですが、やはりわかりません。

ご協力いただければ幸いです!コードスニペットで

/** 
* Android Drive Quickstart activity. This activity takes a photo and saves it 
* in Google Drive. The user is prompted with a pre-made dialog which allows 
* them to choose the file location. 
*/ 
public class Main2Activity extends Activity implements ConnectionCallbacks, 
     OnConnectionFailedListener { 

    private static final String TAG = "drive-quickstart"; 
    private static final int REQUEST_CODE_CAPTURE_IMAGE = 1; 
    private static final int REQUEST_CODE_CREATOR = 2; 
    private static final int REQUEST_CODE_RESOLUTION = 3; 

    private GoogleApiClient mGoogleApiClient; 
    private Bitmap mBitmapToSave; 


    /** 
    * Create a new file and save it to Drive. 
    */ 

    private void saveFileToDrive() { 
     // Start by creating a new contents, and setting a callback. 
     Log.i(TAG, "Creating new contents."); 
     final Bitmap image = mBitmapToSave; 
     Drive.DriveApi.newDriveContents(mGoogleApiClient) 
       .setResultCallback(new ResultCallback<DriveContentsResult>() { 

        @Override 
        public void onResult(DriveContentsResult result) { 
         // If the operation was not successful, we cannot do anything 
         // and must 
         // fail. 
         if (!result.getStatus().isSuccess()) { 
          Log.i(TAG, "Failed to create new contents."); 
          return; 
         } 
         // Otherwise, we can write our data to the new contents. 
         Log.i(TAG, "New contents created."); 
         // Get an output stream for the contents. 
         OutputStream outputStream = result.getDriveContents().getOutputStream(); 
         // Write the bitmap data from it. 
         ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream(); 
         image.compress(Bitmap.CompressFormat.PNG, 100, bitmapStream); 
         try { 
          outputStream.write(bitmapStream.toByteArray()); 
         } catch (IOException e1) { 
          Log.i(TAG, "Unable to write file contents."); 
         } 
         // Create the initial metadata - MIME type and title. 
         // Note that the user will be able to change the title later. 
         MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder() 
           .setMimeType("image/jpeg").setTitle("Android Photo.png").build(); 
         // Create an intent for the file chooser, and start it. 
         IntentSender intentSender = Drive.DriveApi 
           .newCreateFileActivityBuilder() 
           .setInitialMetadata(metadataChangeSet) 
           .setInitialDriveContents(result.getDriveContents()) 
           .build(mGoogleApiClient); 
         try { 
          startIntentSenderForResult(
            intentSender, REQUEST_CODE_CREATOR, null, 0, 0, 0); 
         } catch (SendIntentException e) { 
          Log.i(TAG, "Failed to launch file chooser."); 
         } 
        } 
       }); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     if (mGoogleApiClient == null) { 
      // Create the API client and bind it to an instance variable. 
      // We use this instance as the callback for connection and connection 
      // failures. 
      // Since no account name is passed, the user is prompted to choose. 
      mGoogleApiClient = new GoogleApiClient.Builder(this) 
        .addApi(Drive.API) 
        .addScope(Drive.SCOPE_FILE) 
        .addConnectionCallbacks(this) 
        .addOnConnectionFailedListener(this) 
        .build(); 
     } 
     // Connect the client. Once connected, the camera is launched. 
     mGoogleApiClient.connect(); 
    } 

    @Override 
    protected void onPause() { 
     if (mGoogleApiClient != null) { 
      mGoogleApiClient.disconnect(); 
     } 
     super.onPause(); 
    } 

    @Override 
    protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) { 
     switch (requestCode) { 
      case REQUEST_CODE_CAPTURE_IMAGE: 
       // Called after a photo has been taken. 
       if (resultCode == Activity.RESULT_OK) { 
        // Store the image data as a bitmap for writing later. 
        mBitmapToSave = (Bitmap) data.getExtras().get("data"); 
       } 
       break; 
      case REQUEST_CODE_CREATOR: 
       // Called after a file is saved to Drive. 
       if (resultCode == RESULT_OK) { 
        Log.i(TAG, "Image successfully saved."); 
        mBitmapToSave = null; 
        // Just start the camera again for another photo. 
        startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), 
          REQUEST_CODE_CAPTURE_IMAGE); 
       } 
       break; 
     } 
    } 

    @Override 
    public void onConnectionFailed(ConnectionResult result) { 
     // Called whenever the API client fails to connect. 
     Log.i(TAG, "GoogleApiClient connection failed: " + result.toString()); 
     if (!result.hasResolution()) { 
      // show the localized error dialog. 
      GoogleApiAvailability.getInstance().getErrorDialog(this, result.getErrorCode(), 0).show(); 
      return; 
     } 
     // The failure has a resolution. Resolve it. 
     // Called typically when the app is not yet authorized, and an 
     // authorization 
     // dialog is displayed to the user. 
     try { 
      result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION); 
     } catch (SendIntentException e) { 
      Log.e(TAG, "Exception while starting resolution activity", e); 
     } 
    } 

    @Override 
    public void onConnected(Bundle connectionHint) { 
     Log.i(TAG, "API client connected."); 
     if (mBitmapToSave == null) { 
      // This activity has no UI of its own. Just start the camera. 
      startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), 
        REQUEST_CODE_CAPTURE_IMAGE); 
      return; 
     } 
     saveFileToDrive(); 
    } 

    @Override 
    public void onConnectionSuspended(int cause) { 
     Log.i(TAG, "GoogleApiClient connection suspended"); 
    } 
} 
+0

何らかの理由でこれを含めることができませんでした –

答えて

0

、あなたはapplication/vnd.google-apps.videoにMIMEタイプを変更しようとすることができるとBitmap型キャストには(言うことができます)byte[]。これには大容量のファイルが含まれているため、AndroidアップロードAPIページに動画のアップロードに関するドキュメントはありません。

これには他の方法もあります。私が提案できるのは、uploading filesでREST呼び出しを利用することです。

動画(通常はファイルサイズが大きい)では、再開可能なアップロードを使用することをおすすめします。それはあなたが再開可能なアップロードを使用するための

の手順を実行する必要がありますいくつかの前提条件を持っているが含まれます:

  1. は再開可能セッションを開始します。アップロードURIに、メタデータがある場合はそれを含む初期要求を行います。

  2. 再開可能セッションURIを保存します。最初の要求の応答で返されたセッションURIを保存します。このセッションで残りのリクエストに使用します。

  3. ファイルをアップロードします。再開可能なセッションURIにメディアファイルを送信します。

詳しい情報は、ドキュメントで見つけることができます。

ハッピーコーディング!

+0

チップをありがとう。私はresuambleコードを理解するのに苦労しています。私は仲間プログラマの笑です。私がコードを書こうとすると指示に従うと、その下に赤い印がついています。いくつかは輸入する必要があります。一部には複数のインポートオプションがあります。少し混乱します:私は –

+0

申し訳ありませんが、私は単に何をすべきか考えていません。私はかなり失われているようです。何かをアップロードすることに関する情報はほとんどなく、最終的に何かを見つけたときには、ビデオとはまったく関係がありません –

関連する問題