Adding a big text file to assets folder

2019-02-18 20:48发布

问题:

I'm developing an Android 2.2 application.

I want to add some big text files (4.5MB or more) to Android project.

First I don't know if I can add such kind of big files to assets folder. But, if I can, is it possible to compress them?

How can I compress files? and decompress?

Any other better way to add big text files to Android project?

Thanks.

回答1:

Files over 1 MB placed in the assets folder won't be readable from your app (It'll throw an exception). This is because they get compressed during the build process, and thus the phone requires substantial resources to uncompress them when on the handset.

I believe you can place them in the raw folder, where they won't get compressed or use an extension that AAPT assumes is already compressed (see here)

However, It's not good having a 4.5 MB text file uncompressed sitting in the APK, It's wasted space that could be handled better. Try thinking about downloading the data on first use instead, or splitting the file into chunks as suggested before so that AAPT can compress it.



回答2:

Another approach is you should copy your file into SD card during the first run using IOUtils. Here also be careful also because if you will copy each byte then more resources will be occupied.

It works for me, I needed to put 30MB large zip file into Assets folder because of Client's requirement.



回答3:

You can, but sometimes it gives problems. You don't have to compress it, because the package itself is compressed (the .APK), in fact, anything that you store in the assets folder is uncompressed when you read it. With regards to the size of the file, you may want to cut it and put smaller parts of the file inside the assets folder.



回答4:

I believe the assets directory (except for raw) is already compressed. Also the Android Market will soon/is allowing apks of 50MB in size. Try it first and then see if you have any problems.



回答5:

You need to do fragmentation work for that 4.5 MB text file. Where you need to split the text file into five files with 1 MB maximum size. Then again you need to rejoin them like this:

        OutputStream databaseOutputStream = new FileOutputStream(outFileName);
        InputStream databaseInputStream;

        byte[] buffer = new byte[1024];
        int length;

        databaseInputStream = myContext.getResources().openRawResource(
                R.raw.outfileaaa);
        while ((length = databaseInputStream.read(buffer)) > 0) {
            databaseOutputStream.write(buffer);
        }
        databaseInputStream.close();


        databaseInputStream = myContext.getResources().openRawResource(
                R.raw.outfileaba);
        while ((length = databaseInputStream.read(buffer)) > 0) {
            databaseOutputStream.write(buffer);
        }
        databaseInputStream.close();
        databaseOutputStream.flush();
        databaseOutputStream.close();