Skip to content

Commit

Permalink
Export DB to external storage
Browse files Browse the repository at this point in the history
  • Loading branch information
ujjwalmaity committed Jan 15, 2020
1 parent e342602 commit abcb2d9
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dev.ujjwal.sqlitedatastorage">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand Down
35 changes: 35 additions & 0 deletions app/src/main/java/dev/ujjwal/sqlitedatastorage/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,20 @@

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.Toast;
import android.widget.EditText;
import android.database.Cursor;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

import dev.ujjwal.sqlitedatastorage.data.StudentDbHelper;
import dev.ujjwal.sqlitedatastorage.data.StudentContract.StudentEntry;

Expand Down Expand Up @@ -152,13 +159,41 @@ public void onClick(View v) {
}
});

//MAKE ATTENDANCE
findViewById(R.id.attendance).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, AttendanceActivity.class);
startActivity(intent);
}
});

//EXPORT DB TO EXTERNAL STORAGE
findViewById(R.id.export).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
File data = Environment.getDataDirectory();
// File sd = Environment.getExternalStorageDirectory();
File sd = getExternalFilesDir("student_attendance");
String currentDBPath = "/data/" + getPackageName() + "/databases/" + "studentAttendance.db";
String backupDBPath = "studentAttendance.db";
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
FileChannel source = null;
FileChannel destination = null;
try {
source = new FileInputStream(currentDB).getChannel();
destination = new FileOutputStream(backupDB).getChannel();
destination.transferFrom(source, 0, source.size());
source.close();
destination.close();
Toast.makeText(getApplicationContext(), "DB Exported!", Toast.LENGTH_LONG).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "DB Not Export!", Toast.LENGTH_LONG).show();
}
}
});
}

public void showMessage(String title, String Message) {
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,4 +106,13 @@
android:layout_centerHorizontal="true"
android:text="ATTENDANCE"
android:textColor="@color/colorButton" />

<Button
android:id="@+id/export"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/attendance"
android:layout_centerHorizontal="true"
android:text="EXPORT"
android:textColor="@color/colorButton" />
</RelativeLayout>

0 comments on commit abcb2d9

Please sign in to comment.