Friday, September 16, 2011

Android Programming - Fast Way Preserving Sqlite Db

Actually this is some snippet I found from browsing, the guy who must be credited to this code is Omar Flores, sorry I can't found your link pal :).
It helping me to copy already made SQLite database to my application, it make my job simpler, do not need to fill the database by coding in my Db Helper. And it really fast too, no lag in the runtime.
public class assets extends Activity
 {
        @Override
        protected void onCreate(Bundle icicle)    {
            super.onCreate(icicle);
            // use your own layout :p
            setContentView(R.layout.main);
            try {
                // Open the file
                InputStream in = getAssets().open("yourDB");
                // Open the output file, remember the package name :)
                String outFilename = "/data/data/com.assets/databases/yourDB";
                OutputStream out = new FileOutputStream(outFilename);
                // Transfer bytes from the input file to the output file
                byte[] buf = new byte[1024];
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                // Close the streams
                out.close();
                in.close();
            } catch (IOException e) {
                }
            Toast(this, "File Copied", TOAST.SHORT).show;
        }

 }


Simple code isn't it? But really helpful for me. In my application then I make some additional code to make it just run once, when application first started after install. ^_^

Categories: , , , , ,

0 komentar:

Copyright © Johannes Dwi Cahyo | Powered by Blogger

Design by Anders Noren | Blogger Theme by NewBloggerThemes.com | BTheme.net      Up ↑