問題描述
我使用的是 Android 1.5 我的數(shù)據(jù)目錄沒有讀/寫權限
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());
所以請建議我如何為數(shù)據(jù)目錄提供權限.
So please suggest me how to provide permissions for the data directory.
我要做的是創(chuàng)建一個文件并在模擬器的數(shù)據(jù)存儲中添加一些內(nèi)容,如下所示
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());
}
}
推薦答案
您不應該查看數(shù)據(jù)目錄.這是手機存儲中的系統(tǒng)目錄 - 通常是 /data
- 您的應用程序永遠不會有寫入權限.
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.
您的應用程序應該寫入文件的目錄由 返回Context.getFilesDir()
方法.它將類似于 /data/data/com.yourdomain.YourApp/files
.
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
.
如果您想寫入手機存儲中的文件,請使用 Context.openFileOutput()
方法.
If you want to write to a file in the phone's storage use the Context.openFileOutput()
method.
如果您想要 SDCard 的路徑,請使用 Environment.getExternalStorageDirectory()
方法.要寫入 SDCard,您需要通過將以下內(nèi)容添加到您的 Manifest 來為您的應用程序授予適當?shù)臋嘞?
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" />
如果您要寫入 SDCard,您還需要使用 getExternalStorageState()
方法檢查其狀態(tài).
If you're going to write to the SDCard you'll also need to check its state with the getExternalStorageState()
method.
如果您要存儲與應用程序相關的小文件,那么這些文件可以進入手機的存儲空間而不是 SD 卡,因此請使用 Context.openFileOutput()
和 Context.openFileInput()
方法.
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.
所以在您的代碼中考慮如下內(nèi)容:
So in your code consider something like:
OutputStream os = openFileOutput("samplefile.txt", MODE_PRIVATE);
BufferedWriter lout = new BufferedWriter(new OutputStreamWriter(os));
這篇關于數(shù)據(jù)目錄在Android中沒有讀/寫權限的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!