首页 新闻 赞助 找找看

android中往sdcard写数据问题

0
悬赏园豆:30 [已解决问题] 解决于 2014-10-03 22:53

文件存在,但是无法写入,也捕获不到异常。文件路径:/storage/sdcard/FileDemo1.txt   代码如下:

 1 FileOutputStream fos = null;
 2                 File file1 = new File(root, "FileDemo1.txt");
 3                 try {
 4                     if(file1.exists()){
 5                         try {
 6                             fos = openFileOutput("FileDemo1.txt", MODE_PRIVATE);
 7                             byte[] a = content.getText().toString().getBytes();
 8                             fos.write(a);
 9                             fos.flush();
10                         } catch (Exception e) {
11                             // TODO: handle exception
12                             e.printStackTrace();
13                         } finally {
14                             if(fos != null){
15                                 try {
16                                     fos.close();
17                                 } catch (IOException e) {
18                                     // TODO Auto-generated catch block
19                                     e.printStackTrace();
20                                 }
21                             }
22                         }
23                     }
菜鸟快跑的主页 菜鸟快跑 | 初学一级 | 园豆:6
提问于:2014-10-03 12:19
< >
分享
最佳答案
0

问题找到了,往sdcard写文件必须用绝对路径创建输出流,上面代码的这种方式把文件创建在了/data/data/files/下面。

应该用fos = new FileOutputStream(file1);

菜鸟快跑 | 初学一级 |园豆:6 | 2014-10-03 22:53
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册