Autore Topic: Copiare dei database dalla cartella Assets in data/data/package_name/databases  (Letto 427 volte)

Offline Dani77

  • Nuovo arrivato
  • *
  • Post: 23
  • Respect: 0
    • daniele9475
    • Mostra profilo
  • Dispositivo Android:
    Samsung Galaxy
  • Sistema operativo:
    Windows 8
Ok raggazzi ho visto alcuni post ... io sto facendo cosi ...

Codice (Java): [Seleziona]
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.view.View.OnClickListener;
                 
        public class AssetDatabaseOpenHelper {
         
            private static final String DB_NAME = "danitest.db";
         
            private Context context;
         
            public AssetDatabaseOpenHelper(OnClickListener onClickListener) {
                this.context = (Context) onClickListener;
            }
         
            public SQLiteDatabase openDatabase() {
                File dbFile = context.getDatabasePath(DB_NAME);
         
                if (!dbFile.exists()) {
                    try {
                        copyDatabase(dbFile);
                    } catch (IOException e) {
                        throw new RuntimeException("Error creating source database", e);
                    }
                }
         
                return SQLiteDatabase.openDatabase(dbFile.getPath(), null, SQLiteDatabase.OPEN_READONLY);
            }
         
            private void copyDatabase(File dbFile) throws IOException {
                InputStream is = context.getAssets().open(DB_NAME);
                OutputStream os = new FileOutputStream(dbFile);
         
                byte[] buffer = new byte[1024];
                while (is.read(buffer) > 0) {
                    os.write(buffer);
                }
         
                os.flush();
                os.close();
                is.close();
            }
         
        }


Qui si trova il database...




Infine inizializzo la procedura :

Codice (Java): [Seleziona]
public class javapage1 extends Activity {
               
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.newinterface);
            Button btnHome=(Button)findViewById(R.id.button1);
            btnHome.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View arg0) {
                        AssetDatabaseOpenHelper adb = new AssetDatabaseOpenHelper(this);
                        SQLiteDatabase db = adb.openDatabase();
                        Cursor c = db.rawQuery("SELECT * FROM xxx;", null);
                        Log.d("MyApp", "cnt: "+c.getCount());
                }
            });
        }
        }
         
Infine il log Cat..

08-03 08:27:11.759: D/AndroidRuntime(816): Shutting down VM
08-03 08:27:11.759: W/dalvikvm(816): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-03 08:27:11.809: E/AndroidRuntime(816): FATAL EXCEPTION: main
08-03 08:27:11.809: E/AndroidRuntime(816): java.lang.ClassCastException: package.javapage1$1
08-03 08:27:11.809: E/AndroidRuntime(816):    at package.AssetDatabaseOpenHelper.<init>(AssetDatabaseOpenHelper.java:20)
08-03 08:27:11.809: E/AndroidRuntime(816):    at package.javapage1$1.onClick(javapage1.java:22)
08-03 08:27:11.809: E/AndroidRuntime(816):    at android.view.View.performClick(View.java:2408)
08-03 08:27:11.809: E/AndroidRuntime(816):    at android.view.View$PerformClick.run(View.java:8816)
08-03 08:27:11.809: E/AndroidRuntime(816):    at android.os.Handler.handleCallback(Handler.java:587)
08-03 08:27:11.809: E/AndroidRuntime(816):    at android.os.Handler.dispatchMessage(Handler.java:92)
08-03 08:27:11.809: E/AndroidRuntime(816):    at android.os.Looper.loop(Looper.java:123)
08-03 08:27:11.809: E/AndroidRuntime(816):    at android.app.ActivityThread.main(ActivityThread.java:4627)
08-03 08:27:11.809: E/AndroidRuntime(816):    at java.lang.reflect.Method.invokeNative(Native Method)
08-03 08:27:11.809: E/AndroidRuntime(816):    at java.lang.reflect.Method.invoke(Method.java:521)
08-03 08:27:11.809: E/AndroidRuntime(816):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-03 08:27:11.809: E/AndroidRuntime(816):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-03 08:27:11.809: E/AndroidRuntime(816):    at dalvik.system.NativeStart.main(Native Method)
08-03 08:27:13.659: I/Process(816): Sending signal. PID: 816 SIG: 9


Purtoppo mi da errore..non capisco dovè l'errore help!!!
Daniele

Offline Dani77

  • Nuovo arrivato
  • *
  • Post: 23
  • Respect: 0
    • daniele9475
    • Mostra profilo
  • Dispositivo Android:
    Samsung Galaxy
  • Sistema operativo:
    Windows 8
Re:Copiare dei database dalla cartella Assets in data/data/package_name/databases
« Risposta #1 il: 04 Agosto 2014, 10:51:51 CEST »
0
Risolto grazie.. lo stesso