gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 03:21:44 25 4
gpt4 key购买 nike

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

我遇到的问题是尝试使用变量打开 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 是一个整型数组触发器是在 if else if 设计中创建的 int

...以及尝试...

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

...这将允许加载正确的 Activity ,但 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++ 等,使用变量调用文件非常简单。

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

任何有助于我更好地理解这一点的指导或信息将永远感激。

谢谢,尼克

最佳答案

我编写了以下代码,它对我有用。使用您的特定文件 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();
}
}

关于Android 开发 - 使用变量打开原始文件或 Assets 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19011456/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com