简体   繁体   中英

How to upload a SQLite database file using the latest Google Sign-In and Drive REST api?

Can't find any snippet of code for uploading my database file to Google Drive that's not deprecated.

I've been using Android Studio with bare minimum knowledge in programming using Java, and so far I was able to make an app that can store, edit and delete data using SQLite. Right now I need to do a backup of the database to Google Drive and I have no lead. Here's a code that I've been using with no avail for testing purposes:

    GoogleSignInOptions gso = new     GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .requestProfile()
            .build();

    GoogleSignInClient mGoogleSignInClient = GoogleSignIn.getClient(this, gso);

    Intent signInIntent = mGoogleSignInClient.getSignInIntent();
    startActivityForResult(signInIntent, RC_SIGN_IN);

    Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(this.getIntent());

    try {
        GoogleSignInAccount account = task.getResult(ApiException.class);
        GoogleAccountCredential mCredential = GoogleAccountCredential.usingOAuth2(Home.this.getApplicationContext(), Arrays.asList(new String[]{DriveScopes.DRIVE})).setBackOff(new ExponentialBackOff());
        mCredential.setSelectedAccount(account.getAccount());

        HttpTransport transport = AndroidHttp.newCompatibleTransport();
        JsonFactory jsonFactory = JacksonFactory.getDefaultInstance();
        Drive mService = new com.google.api.services.drive.Drive.Builder(
                transport, jsonFactory, mCredential)
                .setApplicationName(Home.this.getString(R.string.app_name))
                .build();

        com.google.api.services.drive.model.File file = new com.google.api.services.drive.model.File();
        file.setName("test");

        List<String> parents = new ArrayList<>(1);
        parents.add("parent_folder_id"); // Here you need to get the parent folder id
        file.setParents(parents);

        FileContent mediaContent = new FileContent(".txt", File.createTempFile("test", ".txt"));
        mService.files().create(file, mediaContent).setFields("id").execute();
    } catch (ApiException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

So far I've managed to get the log-In screen to popup, but i gives me a NullPointerException on the line that says "mCredential.setSelectedAccount(account.getAccount());".

I have been able to achieve that finally after some hit and trial. Let me know if you need full piece of code. Below snippet is for how to write file content from local db file to Google Drive:

File metadata = new File().setName(Consts.GOOGLE_DRIVE_DBFILE_NAME);
metadata.setMimeType("application/x-sqlite3");
metadata.setDescription("MySugarDiary app backup file");

FileContent mediaContent = new FileContent("application/x-sqlite3", mContext.getDatabasePath(Conts.APP_DB_FILE_NAME));

// Update the metadata and contents.
mDriveService.files().update(fileId, metadata, mediaContent).execute();
Log.d(TAG, "Saved content to: " + Consts.GOOGLE_DRIVE_DBFILE_NAME);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM