How to get the current sqlite database size or package size in Android?
I can't get the total amount of data used by an Android Application (or package), because the official API support has been deleted:
http://groups.google.com/group/android-developers/browse_thread/thread/df94daae34336aad/f96a8425549c6637 (is this still the current situation?)
Getting installed app size (unsopported hack - do not use it)
So is ther开发者_Go百科e any way to get the current sqlite database size? I've found a getMaxSize method or getPageSize but I'm not able to find the total number of pages.
Perhaps someone could verify if this is an acceptable approach, but for a while I was using this:
File f = context.getDatabasePath(dbName);
long dbSize = f.length();
Cheers
You can query the database with these two:
pragma page_size;
pragma page_count;
(see the sqlite pragma docs).
Multiply the first by the second and you'll get total pages used.
You can use this raw query as well, on sqlite 3.16.0+:
SELECT page_count * page_size as size FROM pragma_page_count(), pragma_page_size();
It returns the "size" column with the single row.
Just to add-on about the size.
When I used the code by DroidBot, it works. But the size may not straightaway increase when data is entered into the database (especially if the data is very small). Database will only increment in 1024 bytes (e.g from 8192 bytes to 9216 bytes).
So, to see the database size increase straightaway, try putting in a large amount of data.
精彩评论