写在SD卡中的Andr​​oid文件 [英] Write to a file in SD Card in Android

查看:128
本文介绍了写在SD卡中的Andr​​oid文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着在Android的一个简单的文件写入程序。

I tried a simple file write program in Android.

 try 
            {
                //File f1=new File("/sdcard/Prag.txt");
                File f1=new File("J:\\Prag.txt");
                BufferedWriter out = new BufferedWriter(new FileWriter(f1,true));
                out.write("Pragadheesh" + "\n");
                out.close();

                } catch (IOException ioe) 
                  {ioe.printStackTrace();}

但异常击中。我该如何解决这个问题?

But the exception is getting hit. How can I fix this?


我试图文件F1 =新的文件(Environment.getExternalStorageDirectory()+//Prag.txt);
与F1值= /mnt/sdcard/Prag.txt
但仍然是例外被击中。我在清单文件修改权限过于

[edit]: I tried File f1=new File(Environment.getExternalStorageDirectory() + "//Prag.txt"); and f1 value = /mnt/sdcard/Prag.txt But still the exception gets hit. I have changed the permission too in manifest file

[解决方案]
根据Krishnabhadra的答复,我必须确保你的SD卡未安装在PC上

[solution] As per Krishnabhadra's reply, I had to Make sure your SDCard is not mounted on a PC

推荐答案

Android是基于linux所以windows风格的路径,比如j:\\\\ prag.txt韩元'T的工作。在linux下路径是这样的:/文件夹1 /资料夹/ file.txt的

Android is based on linux so windows style paths like "J:\\prag.txt" won't work. In linux paths are like this: "/folder1/folder2/file.txt"

也不要使用硬codeD的路径,因为不同的手机可能有SD卡不同的道路。因此,你应该使用 Environment.getExternalStorageDirectory(),让到SD卡的路径编程。

Also don't use hardcoded paths, because different phones might have different path for the SD card. Therefore you you should use Environment.getExternalStorageDirectory() that gives the path to the SD card programmatically.

所以使用:

File f1=new File(Environment.getExternalStorageDirectory() + "Prag.txt");

另外通过添加必要的权限:

Also add necessary permission by:

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

里面的清单,外面某处应用标记。

这篇关于写在SD卡中的Andr​​oid文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆