Antoine Murion
Antoine Murion

Reputation: 813

Sample Projects about the backup of Sqlite file and photos on Google Drive programmatically in Android

I am writting an app with an instant messenger function. Users would write text or share images in the IM function. The text data and file name of images are stored in the sqlite file while the images are stored in the device.

Since my server will not keep the data, the user would not get the chat record when he / she switches to a new device. With reference to whatsapp, they allow the users to back up the chat records and images to Google Drive on a regular basis and get the data back from the drive as shown in the picture below.

enter image description here When I go to my own google drive, I would find "Whatsapp" is connected to my google drive as shown below.

enter image description here I am new to the Google Drive API in Android and I would like to backup my sqlite database and images in the same way as Whatsapp does. I know there are few questions related to the back-up of sqlite database to Google Drive but it seems that it only contains part of the codes and this makes the beginner quite difficult to understand.

Will there be any sample projects on github, questions on stackoverflow or tutorials that would allow me learn how to back up sqlite database and images programmatically in Android step by step?

In additions, I am surprised to see only whatsapp is connected to my Google Drive but no more other apps, so I don't know if third-party developers would have the same access to Google Drive and complete the backup in the same way as Whatsapp.

Upvotes: 13

Views: 4534

Answers (2)

ReyAnthonyRenacia
ReyAnthonyRenacia

Reputation: 17651

This is certainly doable. You will need to learn how to upload files to Google Drive programmatically.

Will there be any sample projects on github, questions on stackoverflow or tutorials that would allow me learn how to back up sqlite database and images programmatically in Android step by step?

There are several samples like:

For uploading images, note that you will be using an image mimetype like image/png for .png and image/jpeg for .jpg files. For uploading sql database to Drive, here's a snippet from this sample.

 String mimeType = MimeTypeMap.getSingleton().getExtensionFromMimeType("db");
        MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
                .setTitle(GOOGLE_DRIVE_FILE_NAME) // Google Drive File name
                .setMimeType(mimeType)
                .setStarred(true).build();
        // create a file on root folder
        Drive.DriveApi.getRootFolder(api)
                .createFile(api, changeSet, result.getDriveContents())
                .setResultCallback(backupFileCallback);

Upvotes: 4

Shailendra
Shailendra

Reputation: 213

Have you seen https://github.com/googledrive/android-demos? I think you need to follow "Create a file in App Folder" to save your sqlite file in the user's hidden AppData folder. To retrieve the file back you could query the AppData folder (make sure to call requestSync() before querying to ensure the cache is up-to-date).

Upvotes: 4

Related Questions