How to package files in the/data/packagename/Files folder and break through the uncompress_data_max restrictions

Source: Internet
Author: User
Tags sqlite db

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 );
}

}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.