├── list_arrangement.xml
├── README.md
├── activity_main.xml
├── CloudBackup.java
└── MainActivity.java
/list_arrangement.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
9 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Uploading-SQLite-Database-to-Google-Drive-in-Android
2 | I made a Simple project with a MainActivity.java page using ListView which contains Image and Text Data displayed from SQLite Database. A Button to Upload the SQLite Database to Google Drive.
3 | This is a very Simple project which will help you to get knowledge about Uploading a Database file or any other file to Google Drive from your Android Application
4 |
5 | Steps to Build this Application:
6 | 1) Create a new app in android studio.
7 | 2) Register and "enable" "Google Drive Api" and Google Drive SDK.
8 | 3) Create Java files- MainActivity.java and CloudBackup.java.
9 | 4)Create xml files - activity_main.xml, list_arrangement.xml.
10 | 5) Follow Gradle Libraries or put the code from the build.gradle file.
11 | 6) put all the codes and build Gradle.
12 | 7) Install the app, it will show no data available, then close it and start again, it will show Data is available.
13 | 8) Now Press on The button below the List to create Backup.
14 | 9) Login to your Drive.google.com and check.
15 | 10) You will see your SQLite data base is stored/Uploaded to Google Drive.
16 |
--------------------------------------------------------------------------------
/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
17 |
27 |
35 |
36 |
42 |
43 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
--------------------------------------------------------------------------------
/CloudBackup.java:
--------------------------------------------------------------------------------
1 | package com.example.intuition.humanity;
2 |
3 | import android.app.Activity;
4 | import android.content.Intent;
5 | import android.content.IntentSender.SendIntentException;
6 | import android.os.Bundle;
7 | import android.os.Environment;
8 | import android.support.annotation.NonNull;
9 | import android.util.Log;
10 | import android.webkit.MimeTypeMap;
11 | import android.widget.Toast;
12 |
13 | import com.google.android.gms.common.ConnectionResult;
14 | import com.google.android.gms.common.GoogleApiAvailability;
15 | import com.google.android.gms.common.api.GoogleApiClient;
16 | import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
17 | import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
18 | import com.google.android.gms.common.api.ResultCallback;
19 | import com.google.android.gms.common.api.Status;
20 | import com.google.android.gms.drive.Drive;
21 | import com.google.android.gms.drive.DriveApi;
22 | import com.google.android.gms.drive.DriveApi.DriveContentsResult;
23 | import com.google.android.gms.drive.DriveContents;
24 | import com.google.android.gms.drive.DriveFile;
25 | import com.google.android.gms.drive.DriveFolder;
26 | import com.google.android.gms.drive.MetadataChangeSet;
27 |
28 | import java.io.BufferedInputStream;
29 | import java.io.BufferedOutputStream;
30 | import java.io.File;
31 | import java.io.FileInputStream;
32 | import java.io.FileNotFoundException;
33 | import java.io.IOException;
34 |
35 | import static com.example.intuition.humanity.MainActivity.DATABASE_NAME;
36 | import static com.example.intuition.humanity.MainActivity.PACKAGE_NAME;
37 |
38 | public class CloudBackup extends Activity
39 | implements ConnectionCallbacks, OnConnectionFailedListener {
40 |
41 | private static final String TAG = "drive_Humanity";
42 | private static final int REQUEST_CODE_RESOLUTION = 3;
43 | public static DriveFile mfile;
44 | private static GoogleApiClient mGoogleApiClient;
45 | private static final String DATABASE_PATH = "/data/data/" + PACKAGE_NAME + "/databases/" + DATABASE_NAME;
46 | private static final File DATA_DIRECTORY_DATABASE =
47 | new File(Environment.getDataDirectory() + "/data/" + PACKAGE_NAME + "/databases/" + DATABASE_NAME);
48 | private static final String MIME_TYPE = "application/x-sqlite-3";
49 |
50 | /*
51 | * Create a new file and save it to Drive.
52 | */
53 | public void saveFileToDrive() {
54 | // Start by creating a new contents, and setting a callback.
55 | Log.i(TAG, "Creating new contents.");
56 | Drive.DriveApi.newDriveContents(mGoogleApiClient).setResultCallback(new ResultCallback() {
57 | @Override
58 | public void onResult(@NonNull DriveApi.DriveContentsResult result) {
59 | if (!result.getStatus().isSuccess()) {
60 | Toast.makeText(MainCloudBackup.this, "Error while trying to create new file contents", Toast.LENGTH_LONG).show();
61 | return;
62 | }
63 |
64 | String mimeType = MimeTypeMap.getSingleton().getExtensionFromMimeType(MIME_TYPE);
65 | MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
66 | .setTitle(DATABASE_NAME) // Google Drive File name
67 | .setMimeType(mimeType)
68 | .setStarred(true).build();
69 | // create a file on root folder
70 | Drive.DriveApi.getRootFolder(mGoogleApiClient)
71 | .createFile(mGoogleApiClient, changeSet, result.getDriveContents()).setResultCallback(backupFileCallback);
72 |
73 | }
74 | });
75 | }
76 |
77 | public static void doGDriveBackup() {
78 | Drive.DriveApi.newDriveContents(mGoogleApiClient).setResultCallback(backupContentsCallback);
79 |
80 | }
81 |
82 | static final private ResultCallback backupContentsCallback = new
83 | ResultCallback() {
84 | @Override
85 | public void onResult(DriveApi.DriveContentsResult result) {
86 | if (!result.getStatus().isSuccess()) {
87 | return;
88 | }
89 | String mimeType = MimeTypeMap.getSingleton().getExtensionFromMimeType(MIME_TYPE);
90 | MetadataChangeSet changeSet = new MetadataChangeSet.Builder()
91 | .setTitle(DATABASE_NAME) // Google Drive File name
92 | .setMimeType(mimeType)
93 | .setStarred(true).build();
94 | // create a file on root folder
95 | Drive.DriveApi.getRootFolder(mGoogleApiClient)
96 | .createFile(mGoogleApiClient, changeSet, result.getDriveContents())
97 | .setResultCallback(backupFileCallback);
98 | }
99 | };
100 |
101 | static final private ResultCallback backupFileCallback = new
102 | ResultCallback() {
103 | @Override
104 | public void onResult(DriveFolder.DriveFileResult result) {
105 | if (!result.getStatus().isSuccess()) {
106 | return;
107 | }
108 | mfile = result.getDriveFile();
109 | mfile.open(mGoogleApiClient, DriveFile.MODE_WRITE_ONLY, new DriveFile.DownloadProgressListener() {
110 | @Override
111 | public void onProgress(long bytesDownloaded, long bytesExpected) {
112 | }
113 | }).setResultCallback(backupContentsOpenedCallback);
114 | }
115 | };
116 |
117 | static final private ResultCallback backupContentsOpenedCallback = new
118 | ResultCallback() {
119 | @Override
120 | public void onResult(DriveApi.DriveContentsResult result) {
121 | if (!result.getStatus().isSuccess()) {
122 | return;
123 | }
124 | // DialogFragment_Sync.setProgressText("Backing up..");
125 | DriveContents contents = result.getDriveContents();
126 | BufferedOutputStream bos = new BufferedOutputStream(contents.getOutputStream());
127 | byte[] buffer = new byte[1024];
128 | int n;
129 |
130 | try {
131 | FileInputStream is = new FileInputStream(DATA_DIRECTORY_DATABASE);
132 | BufferedInputStream bis = new BufferedInputStream(is);
133 |
134 | while ((n = bis.read(buffer)) > 0) {
135 | bos.write(buffer, 0, n);
136 | // DialogFragment_Sync.setProgressText("Backing up...");
137 | }
138 | bos.close();
139 | } catch (FileNotFoundException e) {
140 | e.printStackTrace();
141 | } catch (IOException e) {
142 | e.printStackTrace();
143 | }
144 |
145 | contents.commit(mGoogleApiClient, null).setResultCallback(new ResultCallback() {
146 | @Override
147 | public void onResult(Status status) {
148 | // DialogFragment_Sync.setProgressText("Backup completed!");
149 | // mToast(act.getResources().getString(R.string.backupComplete));
150 | // DialogFragment_Sync.dismissDialog();
151 | }
152 | });
153 | }
154 | };
155 |
156 | @Override
157 | public void onBackPressed() {
158 | super.onBackPressed();
159 | Intent i = new Intent(MainCloudBackup.this, MainActivity.class);
160 | startActivity(i);
161 | }
162 |
163 | @Override
164 | protected void onResume() {
165 | super.onResume();
166 | if (mGoogleApiClient == null) {
167 | mGoogleApiClient = new GoogleApiClient.Builder(this)
168 | .addApi(Drive.API).addScope(Drive.SCOPE_FILE).addConnectionCallbacks(this).addOnConnectionFailedListener(this).build();
169 | }
170 | mGoogleApiClient.connect();
171 | }
172 |
173 | @Override
174 | protected void onPause() {
175 | if (mGoogleApiClient != null) {
176 | mGoogleApiClient.disconnect();
177 | }
178 | super.onPause();
179 | }
180 |
181 | @Override
182 | protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
183 | if (resultCode == Activity.RESULT_OK) {
184 | saveFileToDrive();
185 | }
186 |
187 | }
188 |
189 | @Override
190 | public void onConnectionFailed(ConnectionResult result) {
191 | Log.i(TAG, "GoogleApiClient connection failed: " + result.toString());
192 | if (!result.hasResolution()) {
193 | // show the localized error dialog.
194 | GoogleApiAvailability.getInstance().getErrorDialog(this, result.getErrorCode(), 0).show();
195 | return;
196 | }
197 | try {
198 | result.startResolutionForResult(this, REQUEST_CODE_RESOLUTION);
199 | } catch (SendIntentException e) {
200 | Log.e(TAG, "Exception while starting resolution activity", e);
201 | }
202 | }
203 |
204 | @Override
205 | public void onConnected(Bundle connectionHint) {
206 | Log.i(TAG, "API client connected.");
207 |
208 | saveFileToDrive();
209 | // doDriveBackup();
210 | }
211 |
212 | @Override
213 | public void onConnectionSuspended(int cause) {
214 | Log.i(TAG, "GoogleApiClient connection suspended");
215 | }
216 | }
217 |
--------------------------------------------------------------------------------
/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.example.yourprojectname;
2 |
3 | import android.content.ContentValues;
4 | import android.content.Context;
5 | import android.content.Intent;
6 | import android.database.Cursor;
7 | import android.database.sqlite.SQLiteDatabase;
8 | import android.os.Environment;
9 | import android.support.v4.widget.SimpleCursorAdapter;
10 | import android.support.v7.app.AppCompatActivity;
11 | import android.os.Bundle;
12 | import android.util.EventLogTags;
13 | import android.view.View;
14 | import android.webkit.WebView;
15 | import android.widget.AdapterView;
16 | import android.widget.Button;
17 | import android.widget.ListView;
18 | import android.widget.TextView;
19 | import android.widget.Toast;
20 |
21 | import com.google.android.gms.common.api.GoogleApiClient;
22 | import com.google.android.gms.drive.Drive;
23 | import com.google.android.gms.drive.DriveApi;
24 | import com.google.android.gms.drive.DriveContents;
25 | import com.google.android.gms.drive.DriveFolder;
26 | import com.google.android.gms.drive.Metadata;
27 | import com.google.android.gms.drive.MetadataChangeSet;
28 |
29 | import java.io.File;
30 | import java.io.IOException;
31 | import java.io.OutputStream;
32 |
33 |
34 | import com.example.intuition.humanity.ApiClientAsyncTask;
35 |
36 | import static android.os.Build.VERSION_CODES.M;
37 | import static com.example.intuition.humanity.ApiClientAsyncTask.getGoogleApiClient;
38 |
39 | public class MainActivity extends AppCompatActivity {
40 | public static final String PACKAGE_NAME = "com.example.intuition.helpinghand";
41 | public static final String DATABASE_NAME = "orgs_db";
42 | public static final String TABLE_NAME = "orgs_table";
43 | public SQLiteDatabase db = null;
44 | private static final String DATABASE_PATH = "/data/data/" + PACKAGE_NAME + "/databases/" + DATABASE_NAME;
45 | private static final File DATA_DIRECTORY_DATABASE =
46 | new File(Environment.getDataDirectory() + "/data/" + PACKAGE_NAME + "/databases/" + DATABASE_NAME);
47 |
48 | ListView orgs_list1;
49 | private CloudBackup mCloud;
50 |
51 |
52 | @Override
53 | protected void onCreate(Bundle savedInstanceState) {
54 | super.onCreate(savedInstanceState);
55 | setContentView(R.layout.activity_main);
56 |
57 | db = openOrCreateDatabase("orgs_db", Context.MODE_PRIVATE, null);
58 | db.execSQL("CREATE TABLE IF NOT EXISTS orgs_table(_id integer primary key autoincrement unique, orgs_img blob, orgs_name text)");
59 | Cursor cursor = db.rawQuery("SELECT * FROM orgs_table", null);
60 | if (cursor.moveToFirst()){
61 | Toast.makeText(MainActivity.this, "Data is there", Toast.LENGTH_SHORT).show();
62 | }
63 | else {
64 | Toast.makeText(MainActivity.this, "Data is not there", Toast.LENGTH_SHORT ).show();
65 |
66 | ContentValues contentValues = new ContentValues();
67 | contentValues.put("orgs_img", R.drawable.image1);
68 | contentValues.put("orgs_name", "Pizza");
69 | db.insert("orgs_table", null, contentValues);
70 |
71 | ContentValues contentValues1 = new ContentValues();
72 | contentValues1.put("orgs_img", R.drawable.image2);
73 | contentValues1.put("orgs_name", "cheese");
74 | db.insert("orgs_table", null, contentValues1);
75 |
76 | ContentValues contentValues2 = new ContentValues();
77 | contentValues2.put("orgs_img", R.drawable.image3);
78 | contentValues2.put("orgs_name", "Cake");
79 | db.insert("orgs_table", null, contentValues2);
80 |
81 | ContentValues contentValues3 = new ContentValues();
82 | contentValues3.put("orgs_img", R.drawable.image4);
83 | contentValues3.put("orgs_name", "Ice_cream");
84 | db.insert("orgs_table", null, contentValues3);
85 |
86 | ContentValues contentValues4 = new ContentValues();
87 | contentValues4.put("orgs_img", R.drawable.image5);
88 | contentValues4.put("orgs_name", "Coffee");
89 | db.insert("orgs_table", null, contentValues4);
90 |
91 | ContentValues contentValues5 = new ContentValues();
92 | contentValues5.put("orgs_img", R.drawable.image6);
93 | contentValues5.put("orgs_name", "coffee1");
94 | db.insert("orgs_table", null, contentValues5);
95 |
96 | ContentValues contentValues6 = new ContentValues();
97 | contentValues6.put("orgs_img", R.drawable.image7);
98 | contentValues6.put("orgs_name", "Butter");
99 | db.insert("orgs_table", null, contentValues6);
100 |
101 | ContentValues contentValues7 = new ContentValues();
102 | contentValues7.put("orgs_img", R.drawable.image8);
103 | contentValues7.put("orgs_name", "Chicken");
104 | db.insert("orgs_table", null, contentValues7);
105 |
106 | ContentValues contentValues8 = new ContentValues();
107 | contentValues8.put("orgs_img", R.drawable.image9);
108 | contentValues8.put("orgs_name", "Tikka");
109 | db.insert("orgs_table", null, contentValues8);
110 |
111 | ContentValues contentValues9 = new ContentValues();
112 | contentValues9.put("orgs_img", R.drawable.image10);
113 | contentValues9.put("orgs_name", "Curry");
114 | db.insert("orgs_table", null, contentValues9);
115 |
116 | ContentValues contentValues10 = new ContentValues();
117 | contentValues10.put("orgs_img", R.drawable.image11);
118 | contentValues10.put("orgs_name", "Shake");
119 | db.insert("orgs_table", null, contentValues10);
120 |
121 | ContentValues contentValues11 = new ContentValues();
122 | contentValues11.put("orgs_img", R.drawable.image12);
123 | contentValues11.put("orgs_name", "Chocolate");
124 | db.insert("orgs_table", null, contentValues11);
125 | }
126 |
127 | orgs_list1 = (ListView) findViewById(R.id.orgs_list);
128 | final Cursor cursor1 = db.rawQuery("SELECT * FROM orgs_table", null);
129 | String[] from = {"orgs_img", "orgs_name"};
130 | int[] to = {R.id.orgs_img, R.id.orgs_name};
131 | SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(MainActivity.this, R.layout.list_arrangement, cursor1, from, to, 0);
132 | orgs_list1.setAdapter(simpleCursorAdapter);
133 |
134 | orgs_list1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
135 | @Override
136 | public void onItemClick(AdapterView> parent, View v,
137 | int position, long id) {
138 |
139 | TextView tv = (TextView) v.findViewById(R.id.orgs_name);
140 | if (tv.getText().toString().equals("Pizza")) {
141 | Intent intent = new Intent(MainActivity.this, Org1.class);
142 | startActivity(intent);
143 | }
144 | if (tv.getText().toString().equals("cheese")) {
145 | Intent intent = new Intent(MainActivity.this, Org2.class);
146 | startActivity(intent);
147 | }if (tv.getText().toString().equals("Cake")) {
148 | Intent intent = new Intent(MainActivity.this, Org3.class);
149 | startActivity(intent);
150 | }if (tv.getText().toString().equals("Ice_cream")) {
151 | Intent intent = new Intent(MainActivity.this, Org4.class);
152 | startActivity(intent);
153 | }if (tv.getText().toString().equals("Coffee")) {
154 | Intent intent = new Intent(MainActivity.this, Org5.class);
155 | startActivity(intent);
156 | }if (tv.getText().toString().equals("coffee1")) {
157 | Intent intent = new Intent(MainActivity.this, Org6.class);
158 | startActivity(intent);
159 | // }if (tv.getText().toString().equals("Butter")) {
160 | // Intent intent = new Intent(MainActivity.this, Org7.class);
161 | // startActivity(intent);
162 | // }if (tv.getText().toString().equals("Chicken")) {
163 | // Intent intent = new Intent(MainActivity.this, Org8.class);
164 | // startActivity(intent);
165 | // }if (tv.getText().toString().equals("Tikka")) {
166 | // Intent intent = new Intent(MainActivity.this, Org9.class);
167 | // startActivity(intent);
168 | // }if (tv.getText().toString().equals("Curry")) {
169 | // Intent intent = new Intent(MainActivity.this, Org10.class);
170 | // startActivity(intent);
171 | // }if (tv.getText().toString().equals("Shake")) {
172 | // Intent intent = new Intent(MainActivity.this, Org11.class);
173 | // startActivity(intent);
174 | // }if (tv.getText().toString().equals("Chocolate")) {
175 | // Intent intent = new Intent(MainActivity.this, Org12.class);
176 | // startActivity(intent);
177 | }
178 |
179 |
180 | }
181 | });
182 |
183 | Button b1 = (Button) findViewById(R.id.cloud_backup);
184 | b1.setOnClickListener(new View.OnClickListener() {
185 | @Override
186 | public void onClick(View view) {
187 | Toast.makeText(MainActivity.this, "Crash time...", Toast.LENGTH_SHORT).show();
188 | // mCloud.saveFileToDrive();
189 | Intent i = new Intent(MainActivity.this, CloudBackup.class);
190 | startActivity(i);
191 | }
192 | });
193 |
194 | /////////////////////////////////////////////////////////
195 | ////////////For OnClick Webview Open below code///////////
196 |
197 | // orgs_list1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
198 | // @Override
199 | // public void onItemClick(AdapterView> parent, View v,
200 | // int position, long id) {
201 | // WebView webView = new WebView(v.getContext());
202 | // String[] urls = getResources().getStringArray(R.array.bookmark_urls);
203 | // webView.loadUrl(urls[position]);
204 | // }
205 | // });
206 | //////////////////////////////////////////////////////////////////
207 |
208 |
209 | }
210 |
211 |
212 |
213 |
214 | }
215 |
--------------------------------------------------------------------------------