Adding an external database sample (Android external database) at installation _android

Source: Internet
Author: User

Android Packaging installation to join the external database, I have this requirement so I wrote the following code, now share to everyone

Copy Code code as follows:

public void CreateDatabase () {
Try
{
Get the absolute path to the. db file
String databasefilename = Database_path + database_filename;
File dir = new file (rootdirectory);
If the directory does not exist, create this directory
if (!dir.exists ())
Dir.mkdir ();
If the/data/data/org.itec.android.classroom
The. db file does not exist in the directory, then copy the file from the Res\raw directory to the directory
if (!) ( New File (DatabaseFileName)). Exists ()) {
Get the InputStream object that encapsulates the. db file
InputStream is = Getresources (). Openrawresource (R.raw.mydb);
FileOutputStream fos = new FileOutputStream (databasefilename);
byte[] buffer = new byte[7168];
int count = 0;
Start copying. db files
while ((count = is.read (buffer)) > 0) {
Fos.write (buffer, 0, count);
}
Fos.close ();
Is.close ();
}
}
catch (Exception e) {
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.