我想在“ / data”目录中写入文件。我已经植根设备并设置为0。但是,我得到了:
W / System.err:java.io.FileNotFoundException:data / MyDoople.txt (权限被拒绝)
这是我的代码(适用于sdcard):
String filename= "MyDoople.txt";
try
{
File f = new File("data/"+File.separator+filename);
FileOutputStream fOut = new FileOutputStream(f);
OutputStreamWriter myOutWriter = new OutputStreamWriter(
fOut);
myOutWriter.append("Mytest");
myOutWriter.close();
fOut.close();
}
catch(Exception e)
{
e.printStackTrace();
}
我必须添加任何策略以使其起作用吗?
因为数据目录的权限是rwxrwx--x,是用户系统,组系统,并且应用程序的运行用户是普通用户,而不是系统用户,所以应用程序无法读取/ data目录直接。
可以参考两种方式:
1:如果您具有平台证书,请在AndroidManifest.xml中声明android:sharedUserId =“ android.uid.system”,然后使用平台证书对应用重新签名。这样,运行您的应用程序的用户就是系统用户,该用户具有对/ data分区的读写权限。
2:在应用程序中获得root权限,然后执行相关命令,请参阅:
public static boolean RootCommand(String command) {
Process process = null;
DataOutputStream os = null;
try {
process = Runtime.getRuntime().exec("su");
os = new DataOutputStream(process.getOutputStream());
os.writeBytes(command + "\n");
os.writeBytes("exit\n");
os.flush();
process.waitFor();
} catch (Exception e) {
Log.d("*** DEBUG ***", "ROOT REE" + e.getMessage());
return false;
} finally {
try {
if (os != null) {
os.close();
}
process.destroy();
} catch (Exception e) {
}
}
Log.d("*** DEBUG ***", "Root SUC ");
return true;
}