com.google.android.gms.drive.CreateFileActivityOptions Java Examples

The following examples show how to use com.google.android.gms.drive.CreateFileActivityOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RemoteBackup.java    From Database-Backup-Restore with Apache License 2.0 5 votes vote down vote up
private Task<Void> createFileIntentSender(DriveContents driveContents) {

        final String inFileName = activity.getDatabasePath(DATABASE_NAME).toString();

        try {
            File dbFile = new File(inFileName);
            FileInputStream fis = new FileInputStream(dbFile);
            OutputStream outputStream = driveContents.getOutputStream();

            // Transfer bytes from the inputfile to the outputfile
            byte[] buffer = new byte[1024];
            int length;
            while ((length = fis.read(buffer)) > 0) {
                outputStream.write(buffer, 0, length);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        MetadataChangeSet metadataChangeSet = new MetadataChangeSet.Builder()
                .setTitle("database_backup.db")
                .setMimeType("application/db")
                .build();


        CreateFileActivityOptions createFileActivityOptions =
                new CreateFileActivityOptions.Builder()
                        .setInitialMetadata(metadataChangeSet)
                        .setInitialDriveContents(driveContents)
                        .build();

        return mDriveClient
                .newCreateFileActivityIntentSender(createFileActivityOptions)
                .continueWith(
                        task -> {
                            activity.startIntentSenderForResult(task.getResult(), REQUEST_CODE_CREATION, null, 0, 0, 0);
                            return null;
                        });
    }
 
Example #2
Source File: MainActivity.java    From drive-android-quickstart with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an {@link IntentSender} to start a dialog activity with configured {@link
 * CreateFileActivityOptions} for user to create a new photo in Drive.
 */
private Task<Void> createFileIntentSender(DriveContents driveContents, Bitmap image) {
  Log.i(TAG, "New contents created.");
  // Get an output stream for the contents.
  OutputStream outputStream = driveContents.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 e) {
    Log.w(TAG, "Unable to write file contents.", e);
  }

  // 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();
  // Set up options to configure and display the create file activity.
  CreateFileActivityOptions createFileActivityOptions =
      new CreateFileActivityOptions.Builder()
          .setInitialMetadata(metadataChangeSet)
          .setInitialDriveContents(driveContents)
          .build();

  return mDriveClient
      .newCreateFileActivityIntentSender(createFileActivityOptions)
      .continueWith(
          new Continuation<IntentSender, Void>() {
            @Override
            public Void then(@NonNull Task<IntentSender> task) throws Exception {
              startIntentSenderForResult(task.getResult(), REQUEST_CODE_CREATOR, null, 0, 0, 0);
              return null;
            }
          });
}
 
Example #3
Source File: MainActivity.java    From android-samples with Apache License 2.0 5 votes vote down vote up
/**
 * Creates an {@link IntentSender} to start a dialog activity with configured {@link
 * CreateFileActivityOptions} for user to create a new photo in Drive.
 */
private Task<Void> createFileIntentSender(DriveContents driveContents, Bitmap image) {
  Log.i(TAG, "New contents created.");
  // Get an output stream for the contents.
  OutputStream outputStream = driveContents.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 e) {
    Log.w(TAG, "Unable to write file contents.", e);
  }

  // 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();
  // Set up options to configure and display the create file activity.
  CreateFileActivityOptions createFileActivityOptions =
      new CreateFileActivityOptions.Builder()
          .setInitialMetadata(metadataChangeSet)
          .setInitialDriveContents(driveContents)
          .build();

  return mDriveClient
      .newCreateFileActivityIntentSender(createFileActivityOptions)
      .continueWith(
              task -> {
                startIntentSenderForResult(task.getResult(), REQUEST_CODE_CREATOR, null, 0, 0, 0);
                return null;
              });
}
 
Example #4
Source File: CreateFileWithCreatorActivity.java    From android-samples with Apache License 2.0 4 votes vote down vote up
private void createFileWithIntent() {
    // [START drive_android_create_file_with_intent]
    Task<DriveContents> createContentsTask = getDriveResourceClient().createContents();
    createContentsTask
            .continueWithTask(task -> {
                DriveContents contents = task.getResult();
                OutputStream outputStream = contents.getOutputStream();
                try (Writer writer = new OutputStreamWriter(outputStream)) {
                    writer.write("Hello World!");
                }

                MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                                                      .setTitle("New file")
                                                      .setMimeType("text/plain")
                                                      .setStarred(true)
                                                      .build();

                CreateFileActivityOptions createOptions =
                        new CreateFileActivityOptions.Builder()
                                .setInitialDriveContents(contents)
                                .setInitialMetadata(changeSet)
                                .build();
                return getDriveClient().newCreateFileActivityIntentSender(createOptions);
            })
            .addOnSuccessListener(this,
                    intentSender -> {
                        try {
                            startIntentSenderForResult(
                                    intentSender, REQUEST_CODE_CREATE_FILE, null, 0, 0, 0);
                        } catch (IntentSender.SendIntentException e) {
                            Log.e(TAG, "Unable to create file", e);
                            showMessage(getString(R.string.file_create_error));
                            finish();
                        }
                    })
            .addOnFailureListener(this, e -> {
                Log.e(TAG, "Unable to create file", e);
                showMessage(getString(R.string.file_create_error));
                finish();
            });
    // [END drive_android_create_file_with_intent]
}