1313import android .database .Cursor ;
1414import android .os .Bundle ;
1515import android .os .Environment ;
16+ import android .util .Log ;
1617import android .view .Menu ;
1718import android .view .MenuInflater ;
1819import android .view .MenuItem ;
1920import android .widget .Toast ;
2021
22+ import com .opencsv .CSVReader ;
23+
2124import java .io .File ;
25+ import java .io .FileReader ;
2226import java .io .FileWriter ;
2327import java .text .SimpleDateFormat ;
2428import java .util .ArrayList ;
@@ -33,7 +37,8 @@ public class ShowDataActivity extends AppCompatActivity {
3337 List <User > userList ;
3438 DatabaseHelper databaseHelper ;
3539
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 ;
3742
3843
3944 @ SuppressLint ("NotifyDataSetChanged" )
@@ -72,13 +77,15 @@ private void exportCSV() {
7277 }
7378
7479
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";
7683
77- SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd hh:mm:ss a" );
84+ // SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss a");
7885
79- String csvFileName = "database_backup_" +simpleDateFormat .format (new Date ())+".csv" ;
86+ // String csvFileName = "database_backup_"+simpleDateFormat.format(new Date())+".csv";
8087
81- String csvPath = csvFolder . toString () +"/" +csvFileName ;
88+ String csvPath = csvFolder +"/" +csvFileName ;
8289
8390 try {
8491 FileWriter fileWriter = new FileWriter (csvPath );
@@ -108,12 +115,54 @@ private void exportCSV() {
108115
109116 }
110117
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+
111156 public boolean checkStoragePermission (){
112157 return ContextCompat .checkSelfPermission (this , Manifest .permission .WRITE_EXTERNAL_STORAGE ) == PackageManager .PERMISSION_GRANTED ;
113158 }
114159
115160 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 );
117166 }
118167
119168
@@ -137,26 +186,41 @@ public boolean onOptionsItemSelected(@NonNull MenuItem item) {
137186 requestStoragePermission ();
138187 }
139188
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+ }
142198
143199 }
144200
145201 return super .onOptionsItemSelected (item );
146202 }
147203
204+
148205 @ Override
149206 public void onRequestPermissionsResult (int requestCode , @ NonNull String [] permissions , @ NonNull int [] grantResults ) {
150207 super .onRequestPermissionsResult (requestCode , permissions , grantResults );
151208
152- if (requestCode == STORAGE_REQUEST_CODE ){
209+ if (requestCode == EXPORT_STORAGE_REQUEST_CODE ){
153210 if (grantResults .length > 0 && grantResults [0 ] == PackageManager .PERMISSION_GRANTED ){
154- // Export CSV
155211 exportCSV ();
156212 }
157213 else {
158214 Toast .makeText (this , "Storage Permission Needed!" , Toast .LENGTH_SHORT ).show ();
159215 }
160216 }
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+ }
161225 }
162226}
0 commit comments