Jordan Jordan - 7 months ago 27
SQL Question

Download database File For Google Drive API Android

I am using the drive api to create a database file in the hidden app folder on google drive. The database file is called notes.db I have been able to successfully upload the database file to google drive but I have no idea how to download it back to the user's device. This is what i'm trying to do. My app makes a folder on the user's device called School Binder. in that folder is another folder called Note backups. Here is where I backup the database. The directory is

Environment.getExternalStorageDirectory() + "/School Binder/Note Backups/Notes.db"


Google drive takes this file and uploads it to the hidden app folder. Now I want to get this notes.db file stored in that app folder on google drive and download it back to this directory on the phone.

Environment.getExternalStorageDirectory() + "/School Binder/Note Backups/Notes.db"


How do I do this. Thanks. Here is my code for uploading the database to drive this works correctly

// Define And Instantiate Variable DriveContents driveContents//
DriveContents driveContents = result.getStatus().isSuccess() ? result.getDriveContents() : null;

// Gets The Data for The File//
if (driveContents != null) try {

// Define And Instantiate Variable OutputStream outputStream//
OutputStream outputStream = driveContents.getOutputStream();

// Start Writing Data To File//
if (outputStream != null) try {

// Define And Instantiate Variable InputStream inputStream//
InputStream inputStream = new FileInputStream(dbFile);

// Define And Instantiate Variable Byte buffer//
byte[] buffer = new byte[5000];

// Define Variable Int data//
int data;

// Run Code While data Is Bigger Then Zero//
while ((data = inputStream.read(buffer, 0, buffer.length)) > 0) {

// Write To outputStream//
outputStream.write(buffer, 0, data);

// Flush outputStream//
outputStream.flush();
}

} finally {

// Close outputStream//
outputStream.close();
}

} catch (Exception e) {e.printStackTrace(); Toast.makeText(getApplicationContext(), "Failed To Upload: No Backup File Found", Toast.LENGTH_LONG).show(); return;}


How do I change this to make it work for downloading data to a file from google drive

Answer

I figured it out this is my code to redownload a database back to the phone

        //<editor-fold desc="Create Drive Db File On Device">

        // Log That The File Was Opened//
        Log.d("TAG", "File contents opened");

        // Define And Instantiate Variable DriveContents driveContents//
        DriveContents driveContents = result.getStatus().isSuccess() ? result.getDriveContents() : null;

        // Gets The Data for The File//
        if (driveContents != null) try {

            // Define And Instantiate Variable OutputStream outputStream//
            OutputStream outputStream = new FileOutputStream(dbFile);

            // Define And Instantiate Variable InputStream inputStream//
            InputStream inputStream = driveContents.getInputStream();

            // Define And Instantiate Variable Byte buffer//
            byte[] buffer = new byte[5000];

            // Define Variable Int data//
            int data;

            // Run Code While data Is Bigger Then Zero//
            while ((data = inputStream.read(buffer, 0, buffer.length)) > 0) {

                // Write To outputStream//
                outputStream.write(buffer, 0, data);

                // Flush outputStream//
                outputStream.flush();
            }

            // Close outputStream//
            outputStream.close();

            // Discard Drive Contents//
            driveContents.discard(googleApiClient);

        } catch (Exception e) {e.printStackTrace(); Toast.makeText(getApplicationContext(), "File Failed To Download", Toast.LENGTH_LONG).show(); }

        //</editor-fold>
Comments