Android开发 - 使用变量打开原始或资产文件

时间:2013-09-25 17:29:40

标签: android variables assets

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

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

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

// 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 design

中创建的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 ++等,使用变量来调用文件非常简单。

我错过了什么? 在这个过程中我不理解什么?

任何可以帮助我更好地理解这一点的方向或信息都将永远受到赞赏。

谢谢, 尼克

1 个答案:

答案 0 :(得分: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();
    }
}