我没有得到任何例外,但是当我跑步时......
InputStream deckFile = context.getAssets().open("cards.txt");
然后,deckFile.read() 返回 -1。该文件位于正确的文件夹中,并且不为空。
这应该是世界上最简单的事情了……
编辑:AssetManager 确实列出了“cards.txt”,所以这不应该是问题。
我没有得到任何例外,但是当我跑步时......
InputStream deckFile = context.getAssets().open("cards.txt");
然后,deckFile.read() 返回 -1。该文件位于正确的文件夹中,并且不为空。
这应该是世界上最简单的事情了……
编辑:AssetManager 确实列出了“cards.txt”,所以这不应该是问题。
试试下面的代码行
InputStream is = getAssets().open("test.txt");
int size = is.available();
byte[] buffer = new byte[size]; //declare the size of the byte array with size of the file
is.read(buffer); //read file
is.close(); //close file
// Store text file data in the string variable
String str_data = new String(buffer);
可用方法返回资产的总大小......
将您的文本文件放在/assets
Android 项目下的目录中。使用AssetManager
类来访问它。
AssetManager am = context.getAssets();
InputStream is = am.open("test.txt");
或者您也可以将文件放在/res/raw
目录中,该文件将被索引并且可以通过 R 文件中的 id 访问:
InputStream is = getResources().openRawResource(R.raw.test);
编辑:
尝试以下方法来读取您的文件:
public String convertStreamToString(InputStream p_is) throws IOException { /* * To convert the InputStream to String we use the * BufferedReader.readLine() method. We iterate until the BufferedReader * return null which means there's no more data to read. Each line will * appended to a StringBuilder and returned as String. */ if (p_is != null) { StringBuilder m_sb = new StringBuilder(); String m_line; try { BufferedReader m_reader = new BufferedReader( new InputStreamReader(p_is)); while ((m_line = m_reader.readLine()) != null) { m_sb.append(m_line).append("\n"); } } finally { p_is.close(); } Log.e("TAG", m_sb.toString()); return m_sb.toString(); } else { return ""; } }
我相信它会帮助你。
问题是我的文件太大,并且因为它的“.txt”扩展名而被压缩。通过将文件重命名为通常压缩的格式“.mp3”,没有问题