开发者

using big files from res/raw

I am trying to develop dictionary application which uses source files from res/raw. It works when i use a smaller file, but not with a file that I have to use which is cca 5mb.

I decided to split it in parts and to use it this way because it appears that android has some file size limits for assets folder(1mb?). But it doesnt work this way, it loads something in the program but not all the dictionary files. What seems to be the problem? Am I going in a completely wrong way about this?

    private void loadWords() throws IOException {
        final Resources resources = mHelperContext.getResources();
        InputStream inputStream = resources.openRawResource(R.raw.definitions1);
        BufferedReader reader = new BufferedReader(new InputStreamRe开发者_如何学Pythonader(inputStream));
        ucitaj(reader);

        InputStream inputStream2 = resources.openRawResource(R.raw.definitions2);
        BufferedReader reader2 = new BufferedReader(new InputStreamReader(inputStream2));
        ucitaj(reader2);

        InputStream inputStream3 = resources.openRawResource(R.raw.definitions3);
        BufferedReader reader3 = new BufferedReader(new InputStreamReader(inputStream3));
        ucitaj(reader3);

        InputStream inputStream4 = resources.openRawResource(R.raw.definitions4);
        BufferedReader reader4 = new BufferedReader(new InputStreamReader(inputStream4));
        ucitaj(reader4);

        InputStream inputStream5 = resources.openRawResource(R.raw.definitions5);
        BufferedReader reader5 = new BufferedReader(new InputStreamReader(inputStream5));
        ucitaj(reader5);

        InputStream inputStream6 = resources.openRawResource(R.raw.definitions6);
        BufferedReader reader6 = new BufferedReader(new InputStreamReader(inputStream6));
        ucitaj(reader6);

--

  private void ucitaj(BufferedReader reader) throws IOException {
        Log.d(TAG, "Loading words...");
        try {
            String line;
            while ((line = reader.readLine()) != null) {
                String[] strings = TextUtils.split(line, " -- ");
                if (strings.length < 2) continue;
                long id = addWord(strings[0].trim(), strings[1].trim());
                if (id < 0) {
                    Log.e(TAG, "unable to add word: " + strings[0].trim());
                }
            }
        } finally {
            reader.close();
        }
        Log.d(TAG, "DONE loading words.");
    }


I'm pretty sure - that chunking into several resources is useless, since anyway you're reading in the end all files into RAM/heap, which size is limited. In resembling situation (I need to show in TextView relatively large text) I had overcome situation using special class which was implementing CharSequence interface. In fact CharSequence has several abstract methods which can be used for chunking/positioning in big file for necessary fragments, like:

abstract char charAt(int index)
abstract CharSequence subSequence(int start, int end)

Probably in your situtation it might help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜