13
13
import android .database .Cursor ;
14
14
import android .os .Bundle ;
15
15
import android .os .Environment ;
16
+ import android .util .Log ;
16
17
import android .view .Menu ;
17
18
import android .view .MenuInflater ;
18
19
import android .view .MenuItem ;
19
20
import android .widget .Toast ;
20
21
22
+ import com .opencsv .CSVReader ;
23
+
21
24
import java .io .File ;
25
+ import java .io .FileReader ;
22
26
import java .io .FileWriter ;
23
27
import java .text .SimpleDateFormat ;
24
28
import java .util .ArrayList ;
@@ -33,7 +37,8 @@ public class ShowDataActivity extends AppCompatActivity {
33
37
List <User > userList ;
34
38
DatabaseHelper databaseHelper ;
35
39
36
- final static int STORAGE_REQUEST_CODE = 1 ;
40
+ final static int EXPORT_STORAGE_REQUEST_CODE = 1 ;
41
+ final static int IMPORT_STORAGE_REQUEST_CODE = 2 ;
37
42
38
43
39
44
@ SuppressLint ("NotifyDataSetChanged" )
@@ -72,13 +77,15 @@ private void exportCSV() {
72
77
}
73
78
74
79
75
- /// String csvFileName = UUID.randomUUID().toString() + "_database_backup.csv";
80
+ String csvFileName = "SQLite_database_backup.csv" ;
81
+
82
+ // String csvFileName = UUID.randomUUID().toString() + "_database_backup.csv";
76
83
77
- SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss a" );
84
+ // SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
78
85
79
- String csvFileName = "database_backup_" +simpleDateFormat .format (new Date ())+".csv" ;
86
+ // String csvFileName = "database_backup_"+simpleDateFormat.format(new Date())+".csv";
80
87
81
- String csvPath = csvFolder . toString () +"/" +csvFileName ;
88
+ String csvPath = csvFolder +"/" +csvFileName ;
82
89
83
90
try {
84
91
FileWriter fileWriter = new FileWriter (csvPath );
@@ -108,12 +115,54 @@ private void exportCSV() {
108
115
109
116
}
110
117
118
+ private void importCSV () {
119
+
120
+ String importPath = Environment .getExternalStoragePublicDirectory (Environment .DIRECTORY_DOCUMENTS ).getPath ()+"/" +"SQLiteDatabase" +"/" + "SQLite_database_backup.csv" ;
121
+
122
+ File csvFile = new File (importPath );
123
+
124
+ if (csvFile .exists ()){
125
+
126
+ try {
127
+ CSVReader csvReader = new CSVReader (new FileReader (csvFile .getAbsolutePath ()));
128
+
129
+ String [] nextLine ;
130
+
131
+ while ((nextLine = csvReader .readNext ()) != null ){
132
+
133
+ // String id = nextLine[0];
134
+ String name = nextLine [1 ];
135
+ String age = nextLine [2 ];
136
+
137
+ long id = databaseHelper .insertData (name ,age );
138
+
139
+ Log .d ("MSG" ,"" +id );
140
+ }
141
+
142
+ Toast .makeText (this , "Restore Complete" , Toast .LENGTH_SHORT ).show ();
143
+
144
+ }
145
+ catch (Exception e ){
146
+ Toast .makeText (this , "Error: " +e .getMessage (), Toast .LENGTH_SHORT ).show ();
147
+ }
148
+
149
+ }else {
150
+ Toast .makeText (this , "No backup file found!!" , Toast .LENGTH_SHORT ).show ();
151
+ }
152
+
153
+ }
154
+
155
+
111
156
public boolean checkStoragePermission (){
112
157
return ContextCompat .checkSelfPermission (this , Manifest .permission .WRITE_EXTERNAL_STORAGE ) == PackageManager .PERMISSION_GRANTED ;
113
158
}
114
159
115
160
public void requestStoragePermission (){
116
- ActivityCompat .requestPermissions (this , new String []{Manifest .permission .WRITE_EXTERNAL_STORAGE },STORAGE_REQUEST_CODE );
161
+ ActivityCompat .requestPermissions (this , new String []{Manifest .permission .WRITE_EXTERNAL_STORAGE },EXPORT_STORAGE_REQUEST_CODE );
162
+ }
163
+
164
+ public void requestStoragePermissionForImport (){
165
+ ActivityCompat .requestPermissions (this , new String []{Manifest .permission .WRITE_EXTERNAL_STORAGE },IMPORT_STORAGE_REQUEST_CODE );
117
166
}
118
167
119
168
@@ -137,26 +186,41 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
137
186
requestStoragePermission ();
138
187
}
139
188
140
- }else if (id == R .id .menu_restore ){
141
- Toast .makeText (this , "Restore" , Toast .LENGTH_SHORT ).show ();
189
+ }
190
+ else if (id == R .id .menu_restore ){
191
+
192
+ if (checkStoragePermission ()){
193
+ importCSV ();
194
+ }
195
+ else {
196
+ requestStoragePermissionForImport ();
197
+ }
142
198
143
199
}
144
200
145
201
return super .onOptionsItemSelected (item );
146
202
}
147
203
204
+
148
205
@ Override
149
206
public void onRequestPermissionsResult (int requestCode , @ NonNull String [] permissions , @ NonNull int [] grantResults ) {
150
207
super .onRequestPermissionsResult (requestCode , permissions , grantResults );
151
208
152
- if (requestCode == STORAGE_REQUEST_CODE ){
209
+ if (requestCode == EXPORT_STORAGE_REQUEST_CODE ){
153
210
if (grantResults .length > 0 && grantResults [0 ] == PackageManager .PERMISSION_GRANTED ){
154
- // Export CSV
155
211
exportCSV ();
156
212
}
157
213
else {
158
214
Toast .makeText (this , "Storage Permission Needed!" , Toast .LENGTH_SHORT ).show ();
159
215
}
160
216
}
217
+ else if (requestCode == IMPORT_STORAGE_REQUEST_CODE ){
218
+ if (grantResults .length > 0 && grantResults [0 ] == PackageManager .PERMISSION_GRANTED ){
219
+ importCSV ();
220
+ }
221
+ else {
222
+ Toast .makeText (this , "Storage Permission Needed!" , Toast .LENGTH_SHORT ).show ();
223
+ }
224
+ }
161
225
}
162
226
}
0 commit comments