从SD卡读取文件
我已将文件“ecg.text”放入目录 mnt/sdcard/ecg/ecg.text 中,然后使用以下代码来读取它,但不断捕获(FileNotFoundException)。任何有关如何查找该文件的帮助或建议将不胜感激。
提前致谢。
主要活动类:
ECGFilereader reader;
try {
reader = new ECGFilereader("ecg");
reader.ReadFile(waves);
for (int chan=0; chan<ECGFilereader.numChannels; chan++) {
waves[chan].drawSignal(c, (wavePos[chan])); //new Waveform may need to be created
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
和 ECGFilereader.java
public class ECGFilereader {
public final static int numChannels = 12; // the data is stored in 12 channels, one for each lead
public final static int numSamples = 500*6; //500 = fs so *6 for 6 seconds of data
public File file;
private Scanner scanner;
short [] [] ecg = new short [numChannels] [numSamples];
public ECGFilereader (String fname) throws FileNotFoundException
{
File dir = Environment.getExternalStorageDirectory(); //accesses the ecg file from the SD card
file = new File(dir, "mnt/sdcard/ecg/ecg.text");
scanner = new Scanner(file);
}
public boolean ReadFile(Waveform[] waves) // sorts data into and array of an array (12 channels each containing 5000 samples)
{
for (int m=0; m<numSamples && scanner.hasNextInt(); m++)
{
int x = scanner.nextInt();
for (int chan = 0; chan<numChannels && scanner.hasNextInt(); chan++)
{
ecg [chan] [m] = (short) scanner.nextInt();
}
}
for (int chan=0; chan<numChannels; chan++)
waves[chan].setSignal(ecg[chan]); // sets a signl equal to the ecg array of channels
return true;
}
}
I have placed the file "ecg.text" into the directory mnt/sdcard/ecg/ecg.text and am then using the following code to read it but keep getting the catch (FileNotFoundException). Any help or suggestions on how to locate the file would be much appreciated.
Thanks in advance.
the Main activity class:
ECGFilereader reader;
try {
reader = new ECGFilereader("ecg");
reader.ReadFile(waves);
for (int chan=0; chan<ECGFilereader.numChannels; chan++) {
waves[chan].drawSignal(c, (wavePos[chan])); //new Waveform may need to be created
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
and the ECGFilereader.java
public class ECGFilereader {
public final static int numChannels = 12; // the data is stored in 12 channels, one for each lead
public final static int numSamples = 500*6; //500 = fs so *6 for 6 seconds of data
public File file;
private Scanner scanner;
short [] [] ecg = new short [numChannels] [numSamples];
public ECGFilereader (String fname) throws FileNotFoundException
{
File dir = Environment.getExternalStorageDirectory(); //accesses the ecg file from the SD card
file = new File(dir, "mnt/sdcard/ecg/ecg.text");
scanner = new Scanner(file);
}
public boolean ReadFile(Waveform[] waves) // sorts data into and array of an array (12 channels each containing 5000 samples)
{
for (int m=0; m<numSamples && scanner.hasNextInt(); m++)
{
int x = scanner.nextInt();
for (int chan = 0; chan<numChannels && scanner.hasNextInt(); chan++)
{
ecg [chan] [m] = (short) scanner.nextInt();
}
}
for (int chan=0; chan<numChannels; chan++)
waves[chan].setSignal(ecg[chan]); // sets a signl equal to the ecg array of channels
return true;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确保您在 AndroidManifest.xml 中拥有正确的权限。
我相信是:
Make sure you have the right permissions in your AndroidManifest.xml.
I believe it is:
您是否尝试过
file = new File(dir, "ecg/ecg.text");
?但是,我不明白为什么当您甚至没有从磁盘读取文件时(您刚刚创建了一个新的 File 对象),您会收到
FileNotFoundException
。您是否忘记上传某些代码?HTH,
阿克谢
Have you tried
file = new File(dir, "ecg/ecg.text");
?However, I can't see why you would get
FileNotFoundException
when you have not even read the file from the disk (you have just created a new File object). Is there some code that you forgot to upload?HTH,
Akshay