Android raw folder import database

Source: Internet
Author: User
Tags import database

There is such an interview question:

How to publish the SQLite database (dictionary. db file) with the apk file?

A:Put this file in the/res/raw directory. Files in the res \ raw directory are not compressed, so that files in the directory can be extracted directly and the resource id is generated.

So how to import the database under the raw file to the database directory of the installed program?

Public void imporDatabase () {// database directory String dirPath = "/data/com. hkx. wan/databases "; File dir = new File (dirPath); if (! Dir. exists () {dir. mkdir () ;}// database File file = new File (dir, "abc. db"); try {if (! File. exists () {file. createNewFile ();} // load the database InputStream to be imported is = this. getApplicationContext (). getResources (). openRawResource (R. raw. db_weather); FileOutputStream fos = new FileOutputStream (file); byte [] buffere = new byte [is. available ()]; is. read (buffere); fos. write (buffere); is. close (); fos. close ();} catch (FileNotFoundException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();}}


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.