This paper illustrates the method of realizing SD card reading database with Android programming. Share to everyone for your reference, specific as follows:
First add permissions to the Manifest:
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission Android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
Then add the method in Mainactivity:
Sqlitedatabase db; private final String Database_path = android.os.Environment. Getexte
Rnalstoragedirectory (). GetAbsolutePath () + "/vote";
Private String Database_filename = "db_vote.db"; Initialize Database private Sqlitedatabase OpenDatabase () {try {String databasefilename = Database_path + "/" + Database_file
NAME;
File dir = new file (Database_path);
if (!dir.exists ()) Dir.mkdir (); if (!) (
New File (DatabaseFileName)). Exists ()) {InputStream is = Getresources (). Openrawresource (R.raw.db_vote);
FileOutputStream fos = new FileOutputStream (databasefilename);
byte[] buffer = new byte[8192];
int count = 0;
while ((count = is.read (buffer)) > 0) {fos.write (buffer, 0, count);
} fos.close ();
Is.close ();
db = Sqlitedatabase.openorcreatedatabase (databasefilename, NULL);
return DB;
catch (Exception e) {e.printstacktrace ();
return null; }
I hope this article will help you with your Android programming.