从SD卡读取文件

时间:2011-08-16 13:41:02

标签: android file directory sd-card

我已将文件“ecg.text”放入目录mnt / sdcard / ecg / ecg.text中,然后使用以下代码读取它但继续获取catch(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;

}

}

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

你试过file = new File(dir, "ecg/ecg.text");吗?

但是,当你甚至没有从磁盘读取文件(你刚刚创建了一个新的File对象)时,我看不出你为什么会得到FileNotFoundException。是否有一些您忘记上传的代码?

HTH,

阿克沙伊