Android - reading a text file from Assets seems to include a LOT of junk before/after the actual data?

一世执手 提交于 2019-12-06 09:24:40

This is because AssetFileDescriptor.getFileDescriptor() is for your .apk and not the mytextfile.mp3 file inside the .apk. To work with AssetFileDescriptor you need to take e.g. AssetFileDescriptor.getStartOffset() into account as well, which is the offset to the actual file i.e. mytextfile.mp3 in your case.

But there's an easy solution to your problem. Use AssetManager.open(String) instead, which will give you an InputStream to the mytextfile.mp3 file. Like this:

InputStream inputStream = getAssets().open("mytextfile.mp3");
BufferedReader f = new BufferedReader(new InputStreamReader(inputStream));
// ...

Eclipse/ADT occasionally gets the resources corrupted. Try doing a project clean and rebuild to see if that fixes it.

I had the same problem with my app. Try using Apache Commons IO's FileUtils. This adds another 100kb to your apk, but make File handling much easier. And if you store the file as myfile.txt instead of .mp3, does it give the same output?

And did you create the file with a Windows or Linux/Unix System? (And with what application?)

/edit: This works for me:

AssetManager am = this.getAssets();
        InputStream is = am.open("mytextfile.mp3");
        InputStreamReader inputStreamReader = new InputStreamReader(is);
        BufferedReader f = new BufferedReader(inputStreamReader);
        String line = f.readLine();
        while (line != null) {
            // do stuff
            Log.d("TAG", line);
            line = f.readLine();
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!