Android的 - 读从资产中的文本文件,似乎之前,包括垃圾很多/实际数据后? [英] Android - reading a text file from Assets seems to include a LOT of junk before/after the actual data?

查看:92
本文介绍了Android的 - 读从资产中的文本文件,似乎之前,包括垃圾很多/实际数据后?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我包的文本文件和我的Andr​​oid应用程序(在资产),我的应用程序本身的读取。

要避免这个文件被COM pressed,它的命名mytestfile.mp3直到最近,这工作就好了。

在最近的SDK / ADT的变化之一,它似乎什么奇从资产看,我很开放的想法,它是什么时候发生的事情...

我用code这样的事情

  AssetFileDescriptor描述符= getAssets()openFd(mytextfile.mp3);
的BufferedReader F =新的BufferedReader(新的FileReader(descriptor.getFileDescriptor()));
串行= f.readLine();
而(行!= NULL){
    //做的东西
    Log.d(TAG,线);
}

我正在从现在的日志看到的是相当奇怪 - 如果该文件包含像这样

 弗雷德
巴尼
威尔玛

我看到这样的废话海量日志中

<$p$p><$c$c> ߴ ! c W 6 f m >ߩ ' 6 #6 l0 mp

其次 - 最终被我的文字内容

 弗雷德
巴尼
威尔玛

其次是胡言乱语的另一个公吨 - 其中一些看起来像这样

<$p$p><$c$c> 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

正如你所看到的,似乎是从APK原始二进制内容(并没有什么做的文本文件)?

这是一个最近的包装问题,还是我失去了一些东西?我使用ADT15但我没有尝试过的最近的升级,只是还没有!?

P.S。我已经升级到最新的SDK / ADT这个问题仍然存在 - 很明显,我想与谁的过错(不知道如果问题就是Eclipse / ADT / ANT或Android居中)升级它,所以我会开始的想法赏金...


解决方案

这是因为 AssetFileDescriptor.getFileDescriptor()为您的.apk文件,而不是 mytextfile.mp3 中的.apk文件里面。与 AssetFileDescriptor 工作,你需要采取例如 AssetFileDescriptor.getStartOffset()考虑为好,这是在偏移实际的文件即 mytextfile.mp3 您案例。

但有一个简单的解决您的问题。使用 AssetManager.open(字符串)相反,它会给你一个的InputStream mytextfile .MP3 文件。像这样的:

 的InputStream的InputStream = getAssets()开(mytextfile.mp3);
的BufferedReader F =新的BufferedReader(新的InputStreamReader(InputStream的));
// ...

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...

解决方案

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

这篇关于Android的 - 读从资产中的文本文件,似乎之前,包括垃圾很多/实际数据后?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆