2011-08-16 233 views
1

我已經將文件「ecg.text」放入目錄mnt/sdcard/ecg/ecg.text中,然後使用以下代碼來讀取它,但不斷獲取catch(FileNotFoundException) 。任何關於如何找到文件的幫助或建議將不勝感激。從SD卡讀取文件

在此先感謝。

主要活動類:

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; 

} 

}

回答

0

確保您有正確的權限在AndroidManifest.xml中。 我相信它是:

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

此外,在公共ECGFilereader,DIR有「到/ mnt/SD卡/」了,所以你不需要再指定它,當你將值值的文件 – Jan

+0

好的,謝謝,我該如何將它放入清單中,因爲我嘗試過只是粘貼^並且也嘗試了但它似乎沒有任何區別。 – Jacko85

+0

在公共ECGFilereader中,請確保文件=新文件(dir,「/ecg/ecg.text」); – Jan

0

你試過file = new File(dir, "ecg/ecg.text");

但是,當你甚至沒有從磁盤讀取文件(你剛創建一個新的File對象)時,我看不到爲什麼你會得到FileNotFoundException。有沒有忘記上傳的代碼?

HTH,

阿克沙伊

+0

我同意你的FileNotFoundException是奇怪的因爲它在讀取'reader = new ECGFilereader(「ecg」);''的行上進行調試時失敗但那是它應該去的所有代碼。 – Jacko85