从SD卡读取文件

发布于 2024-11-29 17:06:24 字数 1799 浏览 0 评论 0原文

我已将文件“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝颜夕 2024-12-06 17:06:24

确保您在 AndroidManifest.xml 中拥有正确的权限。
我相信是:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Make sure you have the right permissions in your AndroidManifest.xml.
I believe it is:

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
毁梦 2024-12-06 17:06:24

您是否尝试过 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文