簡體   English   中英

從 SD 卡讀取文件

[英]Reading a file from the SD card

我已將文件“ecg.text”放入目錄 mnt/sdcard/ecg/ecg.text 中,然后使用以下代碼讀取它,但不斷得到捕獲 (FileNotFoundException)。 任何有關如何定位文件的幫助或建議將不勝感激。

提前致謝。

主要活動 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();
    }

和 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;

}

}

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

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

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

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

高溫下,

阿克謝

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM