Without transferring the application itself.

The point is that as the application is used, its database replenishes in size, and can occupy a tangible place in the device’s memory, which sometimes leads to known consequences (especially on "stale" devices). Therefore, I want to add the function of transferring the database to the SD card, and continuing to work with it on the SD card.

While writing this idea:

  1. The application, when first started, checks whether the device has an SD card.
  2. If there is an SD card, then the finished sqlite database is immediately unpacked onto the SD card, and the application works from the database on the SD card.
  3. If the SD card is not detected, then it is clear ... but here the ambush described above - over time, there may be a problem with the lack of memory device.
  4. Therefore, in the application I add the function of transferring the database to the SD card by the user's decision (in case the user decided to purchase and install the SD card).

And here's the main question: how to implement the transfer of the working sqlite database from the device’s memory to the SD card, and then continue to work with it?

  • What device are we talking about? Add a tag. - 0xdb
  • the main thing is not to transfer from the kettle :) - gil9red
  • What to do when someone answered your question - pavlofff

1 answer 1

Do this:

  1. The Sqlite database is a regular file, by default it is located in the directory pointed to by the Context.getDatabasePath() method
  2. Copy file using regular java file tools
  3. When copying, you must have permissions for reading / writing to external media and attend to special measures if the API level> = KitKat (google help)
  4. After copying, you specify the location of the database in the overloaded method. You open it using the usual means and use it.

You can use the following method to copy a file:

 public static void copyFile(File src, File dst) throws IOException { FileChannel inChannel = new FileInputStream(src).getChannel(); FileChannel outChannel = new FileOutputStream(dst).getChannel(); try { inChannel.transferTo(0, inChannel.size(), outChannel); } finally { if (inChannel != null) inChannel.close(); if (outChannel != null) outChannel.close(); } } 
  • Hopefully, thanks are allowed in the comments ... - Ayrat
  • Thank you very much. - Ayrat
  • Thank must not so :) - Barmaley