数据目录已经在Android中没有读/写权限 [英] Data directory has no read/write permission in Android
问题描述
我为采用Android 1.5我的数据目录不具有读/写权限
的System.out.println(数据可写? - >中+ Environment.getDataDirectory()canWrite());
的System.out.println(数据读取?? - >中+ Environment.getDataDirectory()的CanRead());
所以,请建议我如何规定的权限数据目录。
我想要做的就是创建一个文件,并添加一些内容,它在仿真器的数据存储像如下
私人无效writeToSDCard(){
尝试 {
文件lroot = Environment.getDataDirectory();
如果(lroot.canWrite()){
文件lfile =新的文件(lroot,samplefile.txt);
FileWriter的lfilewriter =新的FileWriter(lfile);
BufferedWriter将糊涂人=新的BufferedWriter(lfilewriter);
lout.write(XXXXXXXXXXXXXXXXXX);
lout.close();
}
}赶上(IOException异常E){
Log.e(m_cTAG,无法写入文件+ e.getMessage());
}
}
您不应该看的数据目录。这是在手机的存储系统目录 - 通常 /数据
- 和你的应用程序将永远不会有写权限它
您的应用程序应该写文件的目录是由<一回href="http://developer.android.com/reference/android/content/Context.html#getFilesDir%28%29"><$c$c>Context.getFilesDir()$c$c>法。这将是像 /data/data/com.yourdomain.YourApp/files
。
如果你想要写在手机的存储文件使用<一个href="http://developer.android.com/reference/android/content/Context.html#openFileOutput%28java.lang.String,%20int%29"><$c$c>Context.openFileOutput()$c$c>方法。
如果您想要的路径,SD卡,然后使用<一个href="http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory%28%29"><$c$c>Environment.getExternalStorageDirectory()$c$c>法。要写入你需要通过添加以下到你的清单,让您的应用程序的相应权限的SD卡:
&LT;使用-权限的Android:名称=android.permission.WRITE_EXTERNAL_STORAGE/&GT;
如果你要写信给你还需要与 getExternalStorageState()
方法检查其状态的SD卡。
如果你存储的小文件,与你的应用程序,那么这些可以进入手机的存储,而不是SD卡,所以使用 Context.openFileOutput()
和 Context.openFileInput()
的方法。
因此,在您code考虑是这样的:
的OutputStream OS = openFileOutput(samplefile.txt,MODE_PRIVATE);
BufferedWriter将糊涂人=新的BufferedWriter(新OutputStreamWriter(OS));
I m using Android 1.5 my data directory doesn't have the read/write permissions
System.out.println("DAta can write??--->"+Environment.getDataDirectory().canWrite());
System.out.println("DAta can read??--->"+Environment.getDataDirectory().canRead());
So please suggest me how to provide permissions for the data directory.
What I'm trying to do is to create a file and add some content to it in the Data storage of the emulator like as below
private void writeToSDCard() {
try {
File lroot = Environment.getDataDirectory();
if (lroot.canWrite()){
File lfile = new File(lroot, "samplefile.txt");
FileWriter lfilewriter = new FileWriter(lfile);
BufferedWriter lout = new BufferedWriter(lfilewriter);
lout.write("XXXXXXXXXXXXXXXXXX");
lout.close();
}
} catch (IOException e) {
Log.e(m_cTAG, "Could not write file " + e.getMessage());
}
}
You shouldn't be looking at the Data Directory. This is a system directory in the phone's storage - usually /data
- and your application will never have permission to write to it.
The directory your application should write files to is returned by the Context.getFilesDir()
method. It will be something like /data/data/com.yourdomain.YourApp/files
.
If you want to write to a file in the phone's storage use the Context.openFileOutput()
method.
If you want the path to the SDCard then use Environment.getExternalStorageDirectory()
method. To write to the SDCard you'll need to give your application the appropriate permissions by adding the following to your Manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
If you're going to write to the SDCard you'll also need to check its state with the getExternalStorageState()
method.
If you're storing small files to do with your application then these can go into the phone's storage and not the SD Card, so use the Context.openFileOutput()
and Context.openFileInput()
methods.
So in your code consider something like:
OutputStream os = openFileOutput("samplefile.txt", MODE_PRIVATE);
BufferedWriter lout = new BufferedWriter(new OutputStreamWriter(os));
这篇关于数据目录已经在Android中没有读/写权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!