For Android package resources, there are assets during project development, but it is said that they cannot be written.
If you want to break the files that need to be used into the APK file, such as the SQLite dB file, the current method is to first store the file in assets in the project, and then when the program is started for the first time, copy the files in assets to/data.
Note that if you directly use dB as the file suffix, the following occurs when you copy a file larger than 1 MB:
Debug/asset (725): Data exceeds uncompress_data_max (1662976 vs 1048576) This problem occurs because assetsmanager cannot compress and decompress files larger than 1 MB. But the following file types are not compressed because they have been compressed, as follows:/* these formats are already compressed, or don't compress well */static const char * knocompressext [] = {". jpg ",". JPEG ",". PNG ",". GIF ",". wav ",". MP2 ",". MP3 ",". ogg ",". AAC ",". MPG ",". MPEG ",". mid ",". midi ",". SMF ",". jet ",". rtttl ",". IMy ",". xmf ",". MP4 ",". m4a ",". m4v ",". 3GP ",". 3GPP ",". 3g2 ",". 3gpp2 ",". amr ",". AWB ",". WMA ",". WMV "};
Reference: http://ponystyle.com/blog/2010/03/26/dealing-with-asset-compression-in-android-apps/
Therefore, the current idea is to change the SQLite dbfile name to .jpg and put it in assets. Then, when the program is started for the first time, it will be renamed and copied to/data.
The reference code is as follows:
Import Android. content. context;
Import Android. content. res. assetmanager;
Import java. Io. fileoutputstream;
Import java. Io. ioexception;
Import java. Io. inputstream;
Import java. Io. outputstream;
Import Android. util. log;
Public class initresources {
Private context _ context;
Public initresources (context ){
_ Context = context;
}
Public void copyassets (string dirname, string renfrom, string rento ){
Assetmanager = _ context. getassets ();
String [] files = NULL;
Try {
Files = assetmanager. List (dirname );
} Catch (ioexception e ){
Log. E ("tag", E. getmessage ());
}
Inputstream in = NULL;
Outputstream out = NULL;
For (INT I = 0; I <files. length; I ++ ){
Try {
In = assetmanager. Open (dirname + "/" + files [I]);
Out = new fileoutputstream (_ context. getfilesdir (). getabsolutepath () + "/" + Rename (files [I], renfrom, rento ));
Copyfile (In, out );
In. Close ();
In = NULL;
Out. Flush ();
Out. Close ();
Out = NULL;
} Catch (ioexception e ){
Log. E ("tag", files [I]);
Log. E ("tag", E. tostring ());
}
}
}
Private void copyfile (inputstream in, outputstream out) throws ioexception {
Byte [] buffer = new byte [1024];
Int read;
While (read = in. Read (buffer ))! =-1 ){
Out. Write (buffer, 0, read );
}
}
Private string Rename (string filename, string renfrom, string rento)
{
Return filename. replaceall (renfrom, rento );
}
}