2014-09-25 104 views
1

我是新来开发android应用程序。我的第一个项目已经受到挑战。我的应用应该可以通过点击“保存”按钮将EditText字段列表保存到文本文件中。 但是我没有成功将文件写入我的SD卡。如何将文件写入SD卡? (Android)没有错误,但没有写入

我的代码: (在MainActivity.java功能由按钮调用)

public void saveData(View view){ 
     try{ 
     File sdcard = Environment.getExternalStorageDirectory(); 
     // to this path add a new directory path 
     File dir = new File(sdcard.getAbsolutePath() + "/myapp/"); 
     // create this directory if not already created 
     dir.mkdir(); 

     // create the file in which we will write the contents 

     File file = new File(dir, "datei.txt"); 


     FileOutputStream os = new FileOutputStream(file); 
     String data = "some string"; 
     os.write(data.getBytes()); 
     os.flush(); 
     os.close(); 
     } 
     catch (IOException e){ 
      Log.e("com.sarbot.FitLogAlpha", "Cant find Data."); 
     } 
    } 

随着谷歌,我发现另一种方式:

public void saveData3(View view){ 
    FileWriter fWriter; 
    File sdCardFile = new File(Environment.getExternalStorageDirectory() + "/datafile.txt"); 
    Log.d("TAG", sdCardFile.getPath()); //<-- check the log to make sure the path is correct. 
    try{ 
     fWriter = new FileWriter(sdCardFile, true); 
     fWriter.write("CONTENT CONTENT UND SO"); 
     fWriter.flush(); 
     fWriter.close(); 
    }catch(Exception e){ 
       e.printStackTrace(); 
    } 
} 

在我manifest.xml我设置的权限:

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

而功能从开发呃指南返回True - > SD卡是可写的。

/* Checks if external storage is available for read and write */ 
public boolean isExternalStorageWritable() { 
    String state = Environment.getExternalStorageState(); 
    if (Environment.MEDIA_MOUNTED.equals(state)) { 
     return true; 
    } 
    return false; 
} 

res/layout/activity_main.xml一些TextViewsEditText和保存按钮与android:onClick="saveData"说法。该函数被调用。 SD卡是可写的。并没有IO错误。但按下按钮(没有错误)后,我的SD卡上仍然没有新文件。我已经尝试手动创建文件,只是追加,但没有任何更改。我也尝试了BufferedWriter的其他功能..但没有成功。

我使用USB调试模式运行Sony Xperia E。卸载并安装在我的电脑上的SD卡,但无法找到该文件。也许它只能用于手机?它不存在?我不知道该怎么做,因为我没有错误。我需要计算机上的此文件的内容。

:编辑: 问题不在代码中..只是在我抬头的地方。 externalStorage - > SD卡似乎是内部的,可移动的SD卡是 - > ext_card。

+0

os.flush() - 方法刷新此输出流并强制任何缓冲的输出字节被写出。 flush的一般契约是,调用它表明,如果先前写入的任何字节已被输出流的实现缓冲,则应立即将这些字节写入其预定目标。 – 2014-09-25 12:54:28

+0

@Glod Glodson你在哪里找文件?你在哪里搜索?你能告诉我地址​​吗? – mmlooloo 2014-09-25 13:24:18

+0

我在我的电脑上查看了我的可移动SD卡。我已经找到了该文件。 getExternalDirectory()似乎是内部sdcard。混乱。所以sdcard = externalstorage,但是在手机里面,ext_card =可移动的“rly external”sd-card。如果我弄清楚如何访问ext_card,这个问题就解决了。完美将是一种通过电子邮件,Dropbox或其他方式导出文件的方式,但这应该是我猜测的另一个步骤。 – 2014-09-25 13:39:42

回答

0

这个片段此行之后,

File file = new File(dir, "datei.txt"); 

增加这个代码

if (!file.exists()) 
{ 
     file.createNewFile();   // This line will create new blank line. 
} 
+0

我加了这个,但仍然没有改变。有没有可能该文件存在,但我只是没有看到他们与我的电脑上的USB存储模式? – 2014-09-25 13:16:08

+0

可能,做一件事,从USB端口移除手机,然后检查文件。也做一件事,删除旧的应用程序,并重新运行该项目。有时更改不会发生。 – Kedarnath 2014-09-25 13:17:44

+0

我会在我的手机上安装一个文件浏览器来检查它。另一个我已经完成了。 – 2014-09-25 13:20:07

0

os.flush()在您的代码中缺失。添加之前os.close()

+0

我在os.write()后添加了os.flush(),但仍然没有改变 – 2014-09-25 12:54:44