0

I'm trying to send the database file to Google Drive, I imported the libreries necessary, but when I try to upload the file to GoogleDrive I get NPE.

public class Main extends BaseDemoActivity {
private Drive service;

@Override
public void onConnected(Bundle connectionHint) {
    super.onConnected(connectionHint);
    // create new contents resource
    com.google.android.gms.drive.Drive.DriveApi.newDriveContents(getGoogleApiClient())
            .setResultCallback(driveContentsCallback);
}

final private ResultCallback<DriveContentsResult> driveContentsCallback = new
        ResultCallback<DriveContentsResult>() {
            @Override
            public void onResult(final DriveContentsResult result) {
                if (!result.getStatus().isSuccess()) {
                    showMessage("Error while trying to create new file contents");
                    return;
                }

                new Thread() {
                    @Override
                    public void run() {
                         try {
                            java.io.File externalDB = new java.io.File(Environment.getExternalStorageDirectory(), getString(R.string.app_name) + "/data.crypt");
                            java.io.File fileContent = new java.io.File(externalDB.getPath());
                            FileContent mediaContent = new FileContent("application/x-sqlite3", fileContent);

                            com.google.api.services.drive.model.File body =
                                    new com.google.api.services.drive.model.File();
                            body.setTitle("data.crypt");
                            body.setMimeType("application/x-sqlite3");
                            com.google.api.services.drive.model.File file =
                                    service.files().insert(body, mediaContent).execute();
                            if (file != null) {
                                Log.d("GoogleDrive", "File Uploaded");
                            }

                        } catch (UserRecoverableAuthIOException e) {
                            //startActivityForResult(e.getIntent(), REQUEST_AUTHORISATION);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }
                }
                        .start();
            }

        };
...
...

The line of NPE com.google.api.services.drive.model.File file = service.files().insert(body, mediaContent).execute();

Log

    01-17 11:48:32.387 11372-12201/? E/AndroidRuntime: FATAL EXCEPTION: Thread-34061
01-17 11:48:32.387 11372-12201/? E/AndroidRuntime: Process: chris.con, PID: 11372
01-17 11:48:32.387 11372-12201/? E/AndroidRuntime: java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.api.services.drive.Drive$Files com.google.api.services.drive.Drive.files()' on a null object reference
01-17 11:48:32.387 11372-12201/? E/AndroidRuntime:     at chris.con.Main$1$1.run(Main.java:51)
user2847219
  • 555
  • 1
  • 16
  • 27

1 Answers1

0

Well, the log message tells you that your private Drive service is not initialized.

The main issue here is that you're getting into a mess because you are mixing two independent APIs to work with Drive. One is the one from Google Play Sevices with package com.google.android.gms.drive and the other one is from the google-api-java with package com.google.api.services.drive. You should stick to only one of them and on my experience it should be the one of com.google.api.services. The one from Play Services has more bugs, less functionality, and you will often hit rate limits if you want data to be synced when you so require it instead of when the library feels like syncing the data.

You initialize the api.services Drive like this (for example)

GoogleAccountCredential credential = new GoogleAccountCredential(this, "oauth2:" + DriveScopes.DRIVE_FILE);
mDrive = new Drive.Builder(
            new NetHttpTransport(), JacksonFactory.getDefaultInstance(), credential)
            .setApplicationName("Your App")
            .build();

After that you can service.files().insert. You don't need to "create contents" first with the com.google.api.services API.

sorianiv
  • 4,845
  • 1
  • 23
  • 27