[英]Adding full permission for a image file in android.
我正在使用以下代码为android中的图像文件添加完全权限。 这段代码并非每次都起作用。 想象一下,存在三个图像文件A.png,B.png和C.png。 在某些情况下,所有文件都将获得完全许可。 在某些情况下,A.png将获得,在某些情况下,将获得,或者A和B将获得完全许可。 我找不到原因。 请帮助我找到原因。
String iconFile = themePrefs.getString(info.activityInfo.name, ""); // Icon file = "/data/data/com.android.settings/MyApp/A.png";
System.out.println("FileExecute " + ico.canExecute()); //always false
System.out.println("FileRead " + ico.canRead()); //always false
System.out.println("FileWrite " + ico.canWrite()); //always false
System.out.println("FileExists " + ico.exists()); //always true
System.out.println("FileisFile " + ico.isFile()); //always true
System.out.println("FileisDirectory " + ico.isDirectory());//always false
Drawable d = null;
FileInputStream fis; // in stream
try {
File ico = new File(iconFile); //creating a file with the path (because only this is working with absolute path)
try {
Runtime.getRuntime().exec("chmod 777 " + iconFile);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
System.out.println("FileExecute " + ico.canExecute()); // Sometimes getting true, Sometimes false
System.out.println("FileRead " + ico.canRead()); // Sometimes getting true, Sometimes false
System.out.println("FileWrite " + ico.canWrite()); // Sometimes getting true, Sometimes false
System.out.println("FileExists " + ico.exists()); // always true
System.out.println("FileisFile " + ico.isFile()); // always true
System.out.println("FileisDirectory " + ico.isDirectory()); // always false
}
catch (OutOfMemoryError e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch(NullPointerException ne) {
ne.printStackTrace();
}
如果您没有从正在运行的上下文中获得读,写权限,则将无法从同一上下文中执行chmod。 更改为超级用户(例如,为此必须已植根设备),然后可以更改模式。 这是代码。 有用。 我测试了 谢谢
void gainRoot()
{
Process chperm;
try {
chperm=Runtime.getRuntime().exec("su");
DataOutputStream os =
new DataOutputStream(chperm.getOutputStream());
os.writeBytes("chmod 777 /data/data/com.android.settings/MyApp/A.png\n");
os.flush();
os.writeBytes("chmod 777 /data/data/com.android.settings/MyApp/B.png\n");
os.flush();
os.writeBytes("chmod 777 /data/data/com.android.settings/MyApp/C.png\n");
os.flush();
os.writeBytes("exit\n");
os.flush();
chperm.waitFor();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
正确使用代码,并检查您的设备是否已root。 它应该工作。 从oncreate()调用gainRoot()。 如果您有任何困难,请告诉我。 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.