-4

I have created database in my application and stored data in that database now when i run my app in emulater it shows all the data but when i run the same app in my android device it shows the error ? here is my code.

private static String DB_NAME = "pa1.db";
private static String DB_PATH = "/data/data/in.bitcode.sn/databases/";


private void copyDataBase() throws IOException {


    InputStream myInput = context.getAssets().open(DB_NAME);

    String outFileName = DB_PATH + DB_NAME;

    OutputStream myOutput = new FileOutputStream(outFileName);

    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }

    myOutput.flush();
    myOutput.close();
    myInput.close(); 
Pavan Anadkat
  • 111
  • 1
  • 3
  • 9
  • 2
    What's the error? Also, is the database larger than 1MB? Also, don't hardcode paths. – Geobits Oct 16 '12 at 17:47
  • Also, exactly what are you trying to do? – Darwind Oct 16 '12 at 17:49
  • @Geobits DB with larger then 1 MB will make no effect. – Mohsin Naeem Oct 16 '12 at 17:52
  • It will if it's [in the assets folder](http://stackoverflow.com/questions/2860157/load-files-bigger-than-1m-from-assets-folder/3093966#3093966) like he's showing it is. – Geobits Oct 16 '12 at 17:53
  • Database shows in Logcat – Pavan Anadkat Oct 16 '12 at 18:01
  • File Path = Ctxt.getDir("Data", 0); what is "Data" in this ? – Pavan Anadkat Oct 16 '12 at 18:08
  • @Geobits that post is almost 2.5 year old. know things very much chaged. No limit on the file size of the assets. – Mohsin Naeem Oct 16 '12 at 18:08
  • 1
    @MMohsinNaeem Tell that to my users on 2.2 that had their apps crash a couple weeks ago, before I implemented a split/copy. I know it's fixed *now*, since 2.3 in fact. It still crashes on old devices, though, which made it suspicious since he said it worked in the emulator and not on device. Either way, he's said it's only 28kb, so it hardly matters now. Just waiting on the logcat at this point. – Geobits Oct 16 '12 at 18:13
  • 4
    we want logcat, we want logcat ;-D – dorjeduck Oct 16 '12 at 18:14
  • maybe the databases folder is not created the first time. try checking whether the folder exists. And as @dorjeduck has pointed out. post the error, as long as you dont say what error, everyone will be guessing the answers – nandeesh Oct 16 '12 at 20:48
  • there is no error while i am running my app in emulater it shows the whole database and while the same app when i run in my android device it shows Force close – Pavan Anadkat Oct 17 '12 at 05:38

1 Answers1

1

you should not set the database path fixed but use

context.getDatabasePath(DB_NAME)
dorjeduck
  • 7,624
  • 11
  • 52
  • 66