0

I guess many people already read this article:

Using your own SQLite database in Android applications: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/comment-page-2/#comment-12368

However it's keep bringing IOException at

while ((length = myInput.read(buffer))>0){
    myOutput.write(buffer, 0, length);
}

I’am trying to use a large DB file. It’s as big as >8MB I built it using sqlite3 in Mac OS X, inserted UTF-8 encoded strings (for I am using Korean), added android_meta table with ko_KR as locale, as instructed above.

However, When I debug, it keeps showing IOException at

length=myInput.read(buffer)

I suspect it’s caused by trying to read a big file. If not, I have no clue why. I tested the same code using much smaller text file, and it worked fine.

Can anyone help me out on this? I’ve searched many places, but no place gave me the clear answer, or good solution. Good meaning efficient or easy.

I will try use BufferedInput(Output)Stream, but if the simpler one cannot work, I don’t think this will work either.

Can anyone explain the fundamental limits in file input/output in Android, and the right way around it, possibly? I will really appreciate anyone’s considerate answer. Thank you.

WITH MORE DETAIL:

private void copyDataBase() throws IOException{

     //Open your local db as the input stream
     InputStream myInput = myContext.getAssets().open(DB_NAME);

     // Path to the just created empty db
     String outFileName = DB_PATH + DB_NAME;

     //Open the empty db as the output stream
     OutputStream myOutput = new FileOutputStream(outFileName);

     //transfer bytes from the inputfile to the outputfile
     byte[] buffer = new byte[1024];
     int length;
     while ((length = myInput.read(buffer))>0){
      myOutput.write(buffer, 0, length);
     }

     //Close the streams
     myOutput.flush();
     myOutput.close();
     myInput.close();

    }
petershine
  • 3,190
  • 1
  • 25
  • 49
  • Why do you need so much data locally? Seems like a fundamental issue with mobile app development...some Android devices out there still have limited memory, redesign your app so it reads from web and caches a small amount of data locally, 8MB seems like a lot for just one app (considering complex apps like Google Maps take just under 8MB...) – Ricardo Villamil May 28 '10 at 19:24

3 Answers3

2

Can anyone help me out on this?

Use a smaller file. Or a set of smaller files that you stitch together into a large file as you are unpacking them. Or download the database on the first run of your application.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
2

I've been there. I believe the RAW resource type is free of maximum size constraints (whereas Assets, or more specifically, the stream connected to an asset, is limited in size).

Once you get past that hurdle you should be fine - I've had databases on my phone well over 100MB.

Brad Hein
  • 10,997
  • 12
  • 51
  • 74
  • How you using DB do we need to copy DB to data/data/Package_Name/Databases or we can directly use the DB. if yes How ? – k-thorat Nov 30 '10 at 18:29
  • In my situation, I had a database of just under 5 MB. The database was placed in the assets folder with a jpg extension to avoid resource compression. This caused OutOfMemoryErrors on some devices (HTC Wildfire for example). After moving the database to /res/raw and read the file with getResources().openRawResource(R.raw.database), the OutOfMemoryErrors where gone! So thank you very much! – bplayer Aug 10 '12 at 12:59
0

Don't know if it would help you, but if other approaches fail you could try using NIO. For example, here's an implementation of copying inputstream to fileoutput. Alternatively, you could try zipping your file and see if the Android unzipping tools can unzip directly to your db folder.

JRL
  • 76,767
  • 18
  • 98
  • 146