Android creates a database on SDcard

Source: Internet
Author: User

A custom context is used in a database management class, not an app's contextual object:


/** * is used to support access to the database stored on the SD card **/public class DbContext extends Contextwrapper {/** * constructor * * @param base * Context */public DbContext (Context base) {super (base);} /** * Gets the database path, if it does not exist, creates the Object object * * @param name * @param mode * @param factory */@Overridepublic File Getdatabasepath (String Name) {//To determine if there is an SD card Boolean sdexist = Android.os.Environment.MEDIA_MOUNTED.equals ( Android.os.Environment.getExternalStorageState ()); if (!sdexist) {//If not present, LOG.E ("SD card management:", "SD card does not exist, please load SD card"); return null;} else {//if present//Get SD card path string dbdir = Android.os.Environment.getExternalStorageDirectory (). GetAbsolutePath ();d Bdir + = "/database";//the directory where the database is located string dbPath = Dbdir + "/" + name;//Database path//Determine if the directory exists, does not exist then create the directory file Dirfile = new file (Dbdir); Dirfile.exists ()) dirfile.mkdirs ();//The database file is created successfully boolean isfilecreatesuccess = false;//Determines if the file exists and does not exist the file is created DBFile = new File (DbPath), if (!dbfile.exists ()) {try {isfilecreatesuccess = Dbfile.createnewfile ();//Create File} catch (IOException E ) {//TODO auto-generated CATch blocke.printstacktrace ();}} Elseisfilecreatesuccess = true;//Returns the database file object if (isfilecreatesuccess) return Dbfile;elsereturn null;}} /** * Overloads this method, which is used to open the SD card on the database, Android 2.3 and below will call this method. * * @param name * @param mode * @param factory */@Overridepublic sqlitedatabase openorcreatedatabase (String name, int mod E,sqlitedatabase.cursorfactory Factory) {sqlitedatabase result = Sqlitedatabase.openorcreatedatabase ( Getdatabasepath (name), null); return result;} /** * Android 4.0 will call this method to get the database. * * @see android.content.contextwrapper#openorcreatedatabase (java.lang.String, * int, android.database.sqlite.SQLite Database.cursorfactory, * android.database.DatabaseErrorHandler) * @param name * @param mode * @param factory * @para M ErrorHandler */@Overridepublic sqlitedatabase openorcreatedatabase (String name, int mode,cursorfactory Factory, Databaseerrorhandler ErrorHandler) {sqlitedatabase result = Sqlitedatabase.openorcreatedatabase (GetDatabasePath ( name), null); return result;}}



Android creates a database on SDcard

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.