Here's a code snipped that shows how to copy the db from the asset
folder to phone memory:

        private boolean copyDBfromAssets() throws Exception {
                String destPath = "/data/data/" + context.getPackageName() + "/
databases";
        String destPathFull = DB_PATH_PHONE + "/mydatabase";
        InputStream input = null;
                ZipInputStream zipInput = null;
        FileOutputStream output = null;
                try {
                        // open input
                input = context.getAssets().open("database.zip");
                        zipInput = new ZipInputStream(input);

                // create path & file & open output stream
                File outPath = new File(destPath);
                outPath.mkdirs();
                File outFile = new File(destPathFull);
                outFile.createNewFile();
                output = new FileOutputStream( outFile );

                // copy database
                return copyFile(zipInput, output);
                }
                finally {
                        if (zipInput!=null) zipInput.close();
                        if (input!=null) input.close();
                        if (output!=null) output.close();
                }
    }

        private boolean copyFile(InputStream in, FileOutputStream out) throws
IOException {
        byte[] buffer = new byte[4096];
        int bytesRead;
                int sumBytesRead = 0;
        while ((bytesRead = in.read(buffer))>0){
                out.write(buffer, 0, bytesRead);
                sumBytesRead += bytesRead;
        }
        out.flush();
        return sync(out);
        }

To keep the example short I removed all error handling code.

On Apr 27, 8:15 am, Javacoffee <[email protected]> wrote:
> Thanks guys for your kind reply :)
>
> On 4월27일, 오전11시32분, Emanuel Moecklin <[email protected]> wrote:
>
>
>
>
>
>
>
> > I'm running the database initialization in an IntentService which
> > always runs in a separate thread.
> > The main activity starts the service and connects to the service to
> > show a progress bar.
> > If the main activity is shut down the service continues to run.
> > If you restart the main activity it reconnects to the service and
> > shows the progress bar again.
> > The main difficulty with this approach is to prevent the service from
> > being started twice (IntentServices queue the Intents an run them
> > sequentially).
> > I solved this by checking & setting a SharedPreferences value that
> > indicates whether the initialization has finished or not. The Service
> > won't run if it has been previously started.
>
> > I guess that you load the database by sql inserts.
> > I used to do that too but a much faster solution is to include a
> > preloaded database in the asset folder and extract it to phone memory
> > during initialization.
> > This approach works fine if you extract to phone memory and not
> > sdcard. I used to do the latter too but sdcard seems to be too
> > unreliable on some phones and I got too many SQLException crash
> > reports so I reverted back to copying the db to phone memory.
> > One nice side effect of this approach is that the apk will be much
> > smaller (provided the asset database is zipped).
>
> > Cheers
> > Emanuel Moecklin
> > 1gravity LLC
>
> > On Apr 26, 2:37 am, Javacoffee <[email protected]> wrote:
>
> > > Hi guys,
>
> > > I'm implementing the app requires database set-up when installed for
> > > the first time.
>
> > > It works fine however it takes a couple of minutes wait with black
> > > screen (default screen) while establishing database.
>
> > > I understand that is necessary but would like to treat more elegant
> > > approach for User Interface.
>
> > > I'm using DatabaseHelper class inherited from SQLiteOpenHelper to
> > > initiate database for the first time.
>
> > > Here is snippet for your understanding.
>
> > > ================================================
> > > private static class DatabaseHelper extends SQLiteOpenHelper
>
> > >         {
> > >                 private Context dbContext;
>
> > >                 DatabaseHelper(Context context)
> > >                 {
> > >                         super(context, DATABASE_NAME, null, 
> > > DATABASE_VERSION);
> > >                         dbContext = context;
> > >                 }
>
> > >                 @Override
> > >                 public void onCreate(SQLiteDatabase db)
> > >                 {
> > >                         db.execSQL(DATABASE_CREATE);
> > >                         ContentValues values = new ContentValues();
> > >                         loadDB(db, values);
> > >                 }
>
> > >                 @Override
> > >                 public void onUpgrade(SQLiteDatabase db, int oldVersion, 
> > > int
> > > newVersion)
> > >                 {
> > >                         Log.w(TAG, "Upgrading from " + oldVersion + " to 
> > > " + newVersion);
> > >                         db.execSQL(DATABASE_DROP);
> > >                         onCreate(db);
> > >                 }
>
> > >                 private void loadDB(SQLiteDatabase db, ContentValues 
> > > values)
> > >                 {
> > >                         // establishing database here.
> > >                        // it takes a couple of minutes but would like
> > > to handle with more elegant way.
> > >                 }
>
> > > }
>
> > > ====================================================================
>
> > > loadDB() method needs operation with more sophisticated approach so
> > > any idea or advise will be welcome.
>
> > > Thanks.- 원본 텍스트 숨기기 -
>
> > - 원본 텍스트 보기 -

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to