Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.46 KB | None | 0 0
  1. /**
  2. * Copyright 2013 Google Inc. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
  5. * in compliance with the License. You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software distributed under the
  10. * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  11. * express or implied. See the License for the specific language governing permissions and
  12. * limitations under the License.
  13. */
  14.  
  15. package com.google.android.gms.drive.sample.quickstart;
  16.  
  17. import java.io.ByteArrayOutputStream;
  18. import java.io.IOException;
  19. import java.io.OutputStream;
  20.  
  21. import android.app.Activity;
  22. import android.content.Intent;
  23. import android.content.IntentSender;
  24. import android.content.IntentSender.SendIntentException;
  25. import android.graphics.Bitmap;
  26. import android.os.Bundle;
  27. import android.provider.MediaStore;
  28. import android.util.Log;
  29. import android.view.View;
  30. import android.widget.Button;
  31.  
  32. import com.google.android.gms.common.ConnectionResult;
  33. import com.google.android.gms.common.GoogleApiAvailability;
  34. import com.google.android.gms.common.api.GoogleApiClient;
  35. import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
  36. import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
  37. import com.google.android.gms.common.api.ResultCallback;
  38. import com.google.android.gms.drive.Drive;
  39. import com.google.android.gms.drive.DriveApi.DriveContentsResult;
  40. import com.google.android.gms.drive.MetadataChangeSet;
  41.  
  42. /**
  43. * Android Drive Quickstart activity. This activity takes a photo and saves it
  44. * in Google Drive. The user is prompted with a pre-made dialog which allows
  45. * them to choose the file location.
  46. */
  47. public class Main2Activity extends Activity implements ConnectionCallbacks,
  48. OnConnectionFailedListener {
  49.  
  50. private static final String TAG = "drive-quickstart";
  51. private static final int REQUEST_CODE_CAPTURE_IMAGE = 1;
  52. private static final int REQUEST_CODE_CREATOR = 2;
  53. private static final int REQUEST_CODE_RESOLUTION = 3;
  54.  
  55. private GoogleApiClient mGoogleApiClient;
  56. private Bitmap mBitmapToSave;
  57.  
  58.  
  59. /**
  60. * Create a new file and save it to Drive.
  61. */
  62.  
  63. private void saveFileToDrive() {
  64. // Start by creating a new contents, and setting a callback.
  65. Log.i(TAG, "Creating new contents.");
  66. final Bitmap image = mBitmapToSave;
  67. Drive.DriveApi.newDriveContents(mGoogleApiClient)
  68. .setResultCallback(new ResultCallback<DriveContentsResult>() {
  69.  
  70. @Override
  71. public void onResult(DriveContentsResult result) {
  72. // If the operation was not successful, we cannot do anything
  73. // and must
  74. // fail.
  75. if (!result.getStatus().isSuccess()) {
  76. Log.i(TAG, "Failed to create new contents.");
  77. return;
  78. }
  79. // Otherwise, we can write our data to the new contents.
  80. Log.i(TAG, "New contents created.");
  81. // Get an output stream for the contents.
  82. OutputStream outputStream = result.getDriveContents().getOutputStream();
  83. // Write the bitmap data from it.
  84. ByteArrayOutputStream bitmapStream = new ByteArrayOutputStream();
  85. image.compress(Bitmap.CompressFormat.PNG, 100, bitmapStream);
  86. try {
  87. outputStream.write(bitmapStream.toByteArray());
  88. } catch (IOException e1) {
  89. Log.i(TAG, "Unable to write file contents.");
  90. }
  91. // Create the initial metadata - MIME type and title.
  92. // Note that the user will be able to change the title later.
  93. MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
  94. .setMimeType("image/jpeg").setTitle("Android Photo.png").build();
  95. // Create an intent for the file chooser, and start it.
  96. IntentSender intentSender = Drive.DriveApi
  97. .newCreateFileActivityBuilder()
  98. .setInitialMetadata(metadataChangeSet)
  99. .setInitialDriveContents(result.getDriveContents())
  100. .build(mGoogleApiClient);
  101. try {
  102. startIntentSenderForResult(
  103. intentSender, REQUEST_CODE_CREATOR, null, 0, 0, 0);
  104. } catch (SendIntentException e) {
  105. Log.i(TAG, "Failed to launch file chooser.");
  106. }
  107. }
  108. });
  109. }
  110.  
  111. @Override
  112. protected void onResume() {
  113. super.onResume();
  114. if (mGoogleApiClient == null) {
  115. // Create the API client and bind it to an instance variable.
  116. // We use this instance as the callback for connection and connection
  117. // failures.
  118. // Since no account name is passed, the user is prompted to choose.
  119. mGoogleApiClient = new GoogleApiClient.Builder(this)
  120. .addApi(Drive.API)
  121. .addScope(Drive.SCOPE_FILE)
  122. .addConnectionCallbacks(this)
  123. .addOnConnectionFailedListener(this)
  124. .build();
  125. }
  126. // Connect the client. Once connected, the camera is launched.
  127. mGoogleApiClient.connect();
  128. }
  129.  
  130. @Override
  131. protected void onPause() {
  132. if (mGoogleApiClient != null) {
  133. mGoogleApiClient.disconnect();
  134. }
  135. super.onPause();
  136. }
  137.  
  138. @Override
  139. protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
  140. switch (requestCode) {
  141. case REQUEST_CODE_CAPTURE_IMAGE:
  142. // Called after a photo has been taken.
  143. if (resultCode == Activity.RESULT_OK) {
  144. // Store the image data as a bitmap for writing later.
  145. mBitmapToSave = (Bitmap) data.getExtras().get("data");
  146. }
  147. break;
  148. case REQUEST_CODE_CREATOR:
  149. // Called after a file is saved to Drive.
  150. if (resultCode == RESULT_OK) {
  151. Log.i(TAG, "Image successfully saved.");
  152. mBitmapToSave = null;
  153. // Just start the camera again for another photo.
  154. startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE),
  155. REQUEST_CODE_CAPTURE_IMAGE);
  156. }
  157. break;
  158. }
  159. }
  160.  
  161. @Override
  162. public void onConnectionFailed(ConnectionResult result) {
  163. // Called whenever the API client fails to connect.
  164. Log.i(TAG, "GoogleApiClient connection failed: " + result.toString());
  165. if (!result.hasResolution()) {
  166. // show the localized error dialog.
  167. GoogleApiAvailability.getInstance().getErrorDialog(this, result.getErrorCode(), 0).show();
  168. return;
  169. }
  170. // The failure has a resolution. Resolve it.
  171. // Called typically when the app is not yet authorized, and an
  172. // authorization
  173. // dialog is displayed to the user.
  174. try {
  175. result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
  176. } catch (SendIntentException e) {
  177. Log.e(TAG, "Exception while starting resolution activity", e);
  178. }
  179. }
  180.  
  181. @Override
  182. public void onConnected(Bundle connectionHint) {
  183. Log.i(TAG, "API client connected.");
  184. if (mBitmapToSave == null) {
  185. // This activity has no UI of its own. Just start the camera.
  186. startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE),
  187. REQUEST_CODE_CAPTURE_IMAGE);
  188. return;
  189. }
  190. saveFileToDrive();
  191. }
  192.  
  193. @Override
  194. public void onConnectionSuspended(int cause) {
  195. Log.i(TAG, "GoogleApiClient connection suspended");
  196. }
  197. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement