The first impression of an Android app is very important to the user, in addition to having a good creative and beautiful interface, performance is also a key part of this article is to discuss the first startup speed problem. Android app start-up process does not allow users to wait too long, personally feel Best control in 3 seconds of > inside.
Importing a static database in Android is simple, first put the prepared static database files in the Android project's res directory under the raw subdirectory, if there is no such subdirectory, create the directory manually, Then in the initialization phase of the app, copy the database files to a specific directory with code similar to the following , assuming that the Android app's package name is Com.test, then in most cases the default database file for the app is located in/data/data/com.test/ Under the Databases directory.
String Dbdirpath ="/data/data/com.test/databases"; File Dbdir=NewFile (Dbdirpath); if(!dbdir.exists ())//If the directory is not present, create theDbdir.mkdir (); //Open the input stream of a static database fileInputStream is=context.getresources (). Openrawresource (R.raw.data); //Open the output stream for the target database fileFileOutputStream OS =NewFileOutputStream (dbdirpath+"/data.db"); byte[] buffer =New byte[1024x768]; intCount =0; //copy a static database file to a destination while(Count = is. Read (buffer)) >0) {os.write (buffer,0, Count); } is. Close (); Os.close ();
In a recently completed application, with the introduction of a static database, the first boot time from nearly 4 seconds into 1 seconds, the effect is still very obvious.
However, this approach assumes that all Android devices have the same application installation directory, and that the directory for the database files is/data/data/package name/databases, but the Android document does not explicitly stipulate that all devices have this directory structure, So copying a static database file to a pre -determined directory is still a risky practice. A better approach is to use the API provided by the Android system to solve this problem, in short, we have to avoid the use of fixed directory, the following is a better copy process:
//Open the input stream of a static database fileInputStream is=context.getresources (). Openrawresource (R.raw.data); //The context class is used to open the output stream of the target database file, which avoids writing the path to death. FileOutputStream OS = context.openfileinput ("data.db"); byte[] buffer =New byte[1024x768]; intCount =0; //copy a static database file to a destination while(Count = is. Read (buffer)) >0) {os.write (buffer,0, Count); } is. Close (); Os.close ();
The final database file will be located in the/data/data/com.data/files directory, and it is important to note that when using the Openorcreatedatabase method of the context class or the Sqliteopenhelper tool class, The name of the database can no longer be passed as a parameter, but the full path to the database file is passed to it .
Increase your application's first boot speed by importing a static database in Android