将数据记录到外部存储(android)

时间:2014-07-03 13:47:24

标签: android android-external-storage

我遇到了将外部数据存储到外部文本文件中的问题。我的代码如下,但它总是给我IOException案例。我没有看到目录中创建的任何文件。这些应用程序本身就可以关闭,是否可以保持打开状态?为什么呢?

    private void RecordData() {
    // TODO Auto-generated method stub
    myFile = new File("sdcard/suntrackingdata.txt");
    DataSeq = countDis[9] + "," + yawDis[9] + "," + yawdiffDis[9] + "," + AziIniDis[9] + "," + AziFinDis[9] + "," + AxDis[9] + "," + AyDis[9] + "," + ThetaDis[9] + "," + ThetaDiffDis[9] + "," + yawONEdecDis[9] + "," + yawNONdecDis[9] ;
    //if(myFile.exists() == false)
    //{
    //  myFile.mkdirs();
    //}
    try{
    myFile.createNewFile();
    FileOutputStream fos = new FileOutputStream(myFile);
    OutputStreamWriter osw = new OutputStreamWriter(fos);
    osw.append(DataSeq);
    osw.close();
    Toast.makeText(getBaseContext(), "Recording... ",
            Toast.LENGTH_SHORT).show();
    } catch (IOException e)
    {
        Toast.makeText(getBaseContext(), "Failed to write file...", Toast.LENGTH_SHORT).show();
    }
}

当我打开录音时,我在logcat中得到了这个。

    07-03 22:32:33.068: W/System.err(4764): java.io.FileNotFoundException: /sdcard/suntrackingdata.txt: open failed: EISDIR (Is a directory)
07-03 22:32:33.068: W/System.err(4764):     at libcore.io.IoBridge.open(IoBridge.java:409)
07-03 22:32:33.068: W/System.err(4764):     at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
07-03 22:32:33.068: W/System.err(4764):     at com.example.suntracking.MainActivity.RecordData(MainActivity.java:536)
07-03 22:32:33.068: W/System.err(4764):     at com.example.suntracking.MainActivity.access$4(MainActivity.java:526)
07-03 22:32:33.068: W/System.err(4764):     at com.example.suntracking.MainActivity$1$1.run(MainActivity.java:109)
07-03 22:32:33.068: W/System.err(4764):     at android.os.Handler.handleCallback(Handler.java:730)
07-03 22:32:33.068: W/System.err(4764):     at android.os.Handler.dispatchMessage(Handler.java:92)
07-03 22:32:33.068: W/System.err(4764):     at android.os.Looper.loop(Looper.java:137)
07-03 22:32:33.068: W/System.err(4764):     at android.app.ActivityThread.main(ActivityThread.java:5414)
07-03 22:32:33.068: W/System.err(4764):     at java.lang.reflect.Method.invokeNative(Native Method)
07-03 22:32:33.068: W/System.err(4764):     at java.lang.reflect.Method.invoke(Method.java:525)
07-03 22:32:33.068: W/System.err(4764):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
07-03 22:32:33.068: W/System.err(4764):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
07-03 22:32:33.068: W/System.err(4764):     at dalvik.system.NativeStart.main(Native Method)
07-03 22:32:33.068: W/System.err(4764): Caused by: libcore.io.ErrnoException: open failed: EISDIR (Is a directory)
07-03 22:32:33.068: W/System.err(4764):     at libcore.io.Posix.open(Native Method)
07-03 22:32:33.068: W/System.err(4764):     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
07-03 22:32:33.068: W/System.err(4764):     at libcore.io.IoBridge.open(IoBridge.java:393)
07-03 22:32:33.068: W/System.err(4764):     ... 13 more

我的最新代码

    private void RecordData() {
    // TODO Auto-generated method stub
    externalPath = Environment.getExternalStorageDirectory();
    myFile = new File(externalPath,"suntrackingdata.txt");
    DataSeq = countDis[9] + "," + yawDis[9] + "," + yawdiffDis[9] + "," + AziIniDis[9] + "," + AziFinDis[9] + "," + AxDis[9] + "," + AyDis[9] + "," + ThetaDis[9] + "," + ThetaDiffDis[9] + "," + yawONEdecDis[9] + "," + yawNONdecDis[9] ;
    /*if(myFile.exists() == false)
    {
        myFile.mkdirs();
    }*/
    try{
    myFile.createNewFile();
    FileOutputStream fos = new FileOutputStream(myFile,true);
    OutputStreamWriter osw = new OutputStreamWriter(fos);
    BufferedWriter bw = new BufferedWriter(osw);
    bw.write(DataSeq);
    bw.close();
    fos.close();
    Toast.makeText(getBaseContext(), "Recording... ",
            Toast.LENGTH_SHORT).show();
    }catch (FileNotFoundException e)
    {
        Toast.makeText(getBaseContext(), "File Not Found", Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    } catch (IOException e)
    {
        e.printStackTrace();
        Toast.makeText(getBaseContext(), "IOException", Toast.LENGTH_SHORT).show();
    }
}

1 个答案:

答案 0 :(得分:1)

我认为问题在于您指定文件路径的方式。下面是我用来创建文件到外部存储目录的代码片段。

//This line gets the path to your external storage directory.
File externalpath = Environment.getExternalStorageDirectory();

//This line is to specify the filename in the above identified directory.
File newfile = new File(externalpath, "example.txt");

//This if condition will check if the file exists or not and will create an empty file if it doesn't exist.
if (!newfile.exists()) {
            newfile.createNewFile();
}

try {
        FileOutputStream fileoutputstream = new FileOutputStream(newfile);
        BufferedWriter bufferedwrite = new BufferedWriter(
                new OutputStreamWriter(fileoutputstream));

        //you can write however way you want to write in the file 
        bufferedwrite.write("I am writing this file in the external storage");
        bufferedwrite.close();
        fileoutputstream.close();
    } catch (FileNotFoundException e) {
        Toast.makeText(this, "File not found", Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    } catch (IOException e) {
        Toast.makeText(this, "IOexception", Toast.LENGTH_LONG).show();
        e.printStackTrace();
    }