Android - reading a text file from Assets seems to

2019-05-26 04:27发布

问题:

I package a text file with my Android App (in Assets) which I read within the App itself.

To avoid this file being compressed, it's named 'mytestfile.mp3' and until recently, that worked just fine.

In one of the recent SDK/ADT changes, it seems something 'odd' is happening when reading from Assets and I'm open to ideas as to what it is...

I use code something like this

AssetFileDescriptor descriptor = getAssets().openFd("mytextfile.mp3");
BufferedReader f = new BufferedReader(new FileReader(descriptor.getFileDescriptor()));
String line = f.readLine();
while (line != null) {
    // do stuff
    Log.d("TAG",line);
}

What I'm now seeing from the Log is rather odd - if the file contained something like this

Fred
Barney
Wilma

I'm seeing huge amounts of nonsense like this in the log

��ߴ�!�c�W���6�f����m�>ߩ���'�����6�#6���l0��mp�

followed - eventually by my text content

Fred
Barney
Wilma

followed by another metric tonne of gibberish - some of which looks like this

����������4�u?'����������������������������������������res/drawable-mdpi/icon.pngPK��������|v?,������������'�����������������������������res/layout-land/dialog_color_picker.xmlPK��������|v?1�!�����t2�������������������������������classes.dexPK��������|v?թVڝ����5���������������������������������META-INF/MANIFEST.MFPK��������|v?�v������j���������������������������������META-INF/CERT.SFPK��������|v?W7@�]�������������������������������������META-INF/CERT.RSAPK������������������������

As you can see, that appears to be raw binary content from the APK (and nothing to do with the text file)??

Is this a recent packaging issue or am I missing something? I'm using ADT15 but I've not tried the recent upgrade just yet!?

p.s. I've upgraded to the latest SDK/ADT and this problem persists - obviously I'd like to escalate it with whoever is at fault (no idea if the problem is Eclipse/ADT/ANT or Android centered) and so I'll start a bounty for ideas...

回答1:

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));
// ...


回答2:

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



回答3:

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