我正在使用wifi相關的應用程序,它需要用戶提供用戶名密碼,並且需要在位於系統/ etc/wifi /路徑中的文件中寫入(編輯/附加)。如何使用android程序在system/etc/wifi/wpa_supplicant.conf文件中寫入文件?
我在stackoverflow搜索並得到了一些答案,然後嘗試,但沒有運氣。這是根植設備。我也重新安裝了,也是。我可以讀取同一個文件。我也將所有權限都放在清單中。我錯過了什麼?
try {
String[] command = new String[]{"/system/bin/ls", "-l",
"/system/etc/wifi/wpa_supplicant.conf" };
Process process = Runtime.getRuntime().exec(command);
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
int read;
String output = "";
String line;
while ((line = reader.readLine()) != null) {
output.concat(line + "\n");
Log.w("myApp", "[[output]]:" + line);
}
reader.close();
process.waitFor();
String[] command2 = new String[]{"/system/etc/wifi/chmod", "777",
"/system/etc/wifi/wpa_supplicant.conf" };
Runtime rt = Runtime.getRuntime();
Process p = rt.exec(new String[]{"/system/bin/sh", "-c", "echo \"12345678\" > /system/etc/wifi/wpa_supplicant.conf"});
File path= Environment.getDataDirectory();
FileWriter fw = new FileWriter(new File("/system/etc/wifi/wpa_supplicant"));
fw.write('9');
fw.close();
Log.d(TAG, "File Saved Successfully !!!");
//display file saved message
/*Toast.makeText(getBaseContext(), "File saved successfully!",
Toast.LENGTH_SHORT).show();*/
} catch (Exception e) {
e.printStackTrace();
}
這是我的清單權限(外部應用程序標記和一些更多的權限還有一些其他目的)。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
及以下的錯誤我得到,
01-01 06:13:53.565: W/myApp(11205): [[output]]:-rw-rw-rw- root root 58 2000-01-01 03:56 wpa_supplicant.conf
01-01 06:13:53.580: W/System.err(11205): java.io.FileNotFoundException: /system/etc/wifi/wpa_supplicant: open failed: EROFS (Read-only file system)
01-01 06:13:53.580: W/System.err(11205): at libcore.io.IoBridge.open(IoBridge.java:452)
01-01 06:13:53.580: W/System.err(11205): at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
01-01 06:13:53.580: W/System.err(11205): at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
01-01 06:13:53.581: W/System.err(11205): at java.io.FileWriter.<init>(FileWriter.java:42)
01-01 06:13:53.581: W/System.err(11205): at com.intel.sunnypoint.headless.HeadlessService.WriteBtn(HeadlessService.java:450)
01-01 06:13:53.581: W/System.err(11205): at com.intel.sunnypoint.headless.HeadlessService.setupWifiAPConfig(HeadlessService.java:386)
01-01 06:13:53.581: W/System.err(11205): at com.intel.sunnypoint.headless.HeadlessService$HttpServiceThread$WiFiPageHandler.handle(HeadlessService.java:1094)
01-01 06:13:53.581: W/System.err(11205): at org.apache.http.protocol.HttpService.doService(HttpService.java:248)
01-01 06:13:53.581: W/System.err(11205): at org.apache.http.protocol.HttpService.handleRequest(HttpService.java:192)
01-01 06:13:53.581: W/System.err(11205): at com.intel.sunnypoint.headless.HeadlessService$HttpServiceThread.run(HeadlessService.java:663)
01-01 06:13:53.581: W/System.err(11205): Caused by: android.system.ErrnoException: open failed: EROFS (Read-only file system)
01-01 06:13:53.581: W/System.err(11205): at libcore.io.Posix.open(Native Method)
01-01 06:13:53.581: W/System.err(11205): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
01-01 06:13:53.582: W/System.err(11205): at libcore.io.IoBridge.open(IoBridge.java:438)
01-01 06:13:53.582: W/System.err(11205): ... 9 more
感謝您的快速響應,但我需要一個解決方案,以便如何在該特定位置修改文件? – kodali
感謝寶貴的信息,你能指導我如何獲得動態權限嗎?如果你在這個問題上幫助我,你會拯救我的生命。 – kodali