1

我是 Android 开发的新手。我一直在构建一个小型闪存卡应用程序来帮助孩子们阅读,而我想要完成的最终任务是读取文本文件的内容,将其拆分为一个数组,然后在屏幕上显示其中一个单词。比较简单。

我遇到的问题是尝试使用变量在 raw 或 assets 文件夹中打开 .txt 文件。我已经研究并被引导到许多例子,我想我只是不明白在这里做什么。

以下是我在过去 12 小时内所做的各种尝试。我真的可以在这里使用一些蹩脚的术语帮助和方向。我不确定我做错了什么。

// variable for files  in 'raw' directory
Integer[] fileList = { R.raw.animals, R.raw.colors, R.raw.fullwords, R.raw.numbers, R.raw.shapes };



// this is the portion that picks the word and plays the associated audio file
try {
       fileStream = getResources().openRawResource(R.raw.fullwords);
       fileLen = fileStream.available();

       // Read the entire resource into a local byte buffer.
       byte[] fileBuffer = new byte[fileLen];
       fileStream.read(fileBuffer);
       fileStream.close();

       // build text file contents to an array
       displayText = new String(fileBuffer);
       listArray = displayText.split(",");

       // pick a random number
       maxNumber = listArray.length;
       randomNo = 0 + (int)(Math.random() * ((maxNumber - 0) + 1));

       // post to textView of 'textofWord'
       textofWord.setText("");

       // listArray[randomNo]
       textofWord.setText(listArray[randomNo]);

       // create audio file name
       audioFileName = listArray[randomNo] + ".mp3";
       audioFileLocation = "/raw/" + listArray[randomNo];

       // launch the audio file - using SoundPool so it can be clicked multiple times - quickly if they like :)
       wordSound = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);

       // replace "grenade" with listArray[randomNo] when the audio files are made
       int sound_id = this.getResources().getIdentifier("grenade", "raw", this.getPackageName());
       audioWord = wordSound.load(this, sound_id, 1);

        } catch (IOException e) {
          // exception handling
        e.printStackTrace();
        }

我也试过了......

fileStream = getResources().openRawResource(fileList[trigger]);
fileLen = fileStream.available();

fileList 是一个 int 数组触发器是一个在 if else if 设计中创建的 int

......以及尝试......

int id = this.getResources().getIdentifier(value, "raw", this.getPackageName());
fileStream = getResources().openRawResource(id);
fileLen = fileStream.available();

...这将允许加载正确的活动,但出现在 textview 中的文本只是“0”

String FILENAME = value + ".txt";
String collected = null;
FileInputStream fis = null;

// this is the portion that picks the word and plays the associated audio file
try {
fis = openFileInput(FILENAME);
byte[] dataArray = new byte[fis.available()];
while (fis.read(dataArray) != -1) {
collected = new String(dataArray);
}

       // build text file contents to an array
       // displayText = new String(fileBuffer);
       listArray = collected.split(",");

       // pick a random number
       maxNumber = listArray.length;
       randomNo = 0 + (int)(Math.random() * ((maxNumber - 0) + 1));

       // post to textView of 'textofWord'
       textofWord.setText("");

       // listArray[randomNo]
       textofWord.setText(listArray[randomNo]);

我什至尝试重新安排 try catch 框架,但……什么都没有。我开始觉得基于变量加载文件是完全不可能的......

String FILENAME = value + ".txt";
String collected = null;
FileInputStream fis = null;

// this is the portion that picks the word and plays the associated audio file
try {
fis = openFileInput(FILENAME);
byte[] dataArray = new byte[fis.available()];
while (fis.read(dataArray) != -1) {
collected = new String(dataArray);
}

        } catch (FileNotFoundException e) {
        e.printStackTrace();

} catch (IOException e) {
          // exception handling
        e.printStackTrace();

        }   finally {
        try {
        fis.close();

           // build text file contents to an array
           // displayText = new String(fileBuffer);
           listArray = collected.split(",");

           // pick a random number
           maxNumber = listArray.length;
           randomNo = 0 + (int)(Math.random() * ((maxNumber - 0) + 1));

           // post to textView of 'textofWord'
           textofWord.setText("");

           // listArray[randomNo]
           textofWord.setText(collected);

           // create audio file name
           audioFileName = listArray[randomNo] + ".mp3";
           audioFileLocation = "/raw/" + listArray[randomNo];

           // launch the audio file - using SoundPool so it can be clicked multiple times -             quickly if they like :)
           wordSound = new SoundPool(5, AudioManager.STREAM_MUSIC, 0);

           // replace "grenade" with listArray[randomNo] when the audio files are made
           int sound_id = this.getResources().getIdentifier("grenade", "raw", this.getPackageName());
           audioWord = wordSound.load(this, sound_id, 1);

        } catch (IOException e) {
        e.printStackTrace();
        }
        }

我真的很抱歉把它放在那里,但我真的不知道如何使用变量来加载文件名。我使用过其他编程语言,例如 php、sql、一些 c++ 等,并且使用变量来调用文件非常简单。

我错过了什么?在这个过程中我不明白什么?

任何可以帮助我更好地理解这一点的方向或信息将不胜感激。

谢谢,尼克

4

1 回答 1

1

我编写了以下代码,它对我有用。使用您的特定文件 ID 在您的应用程序上运行此代码,看看它是否获取数据 -

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    try {
        //store the ids into array, put your corresponding raw file ids
        int[] resources = {R.raw.hel};
        InputStream is = getResources().openRawResource(resources[0]);
        Log.d("demo" , "available bytes = " + is.available());
        byte[] buffer = new byte[100];
        int readCount = is.read(buffer);
        Log.d("demo", "Read data = "+new String(buffer, 0, readCount));
    } catch (IOException e) {
        e.printStackTrace();
    }
}
于 2013-09-25T19:30:33.343 回答