在android中添加图像文件的完全权限。

在android中添加图像文件的完全权限。,android,file,Android,File,我使用下面的代码为android中的图像文件添加完全权限。 此代码并非每次都有效。假设有三个图像文件,A.png、B.png和C.png。在某些情况下,所有文件都将获得完全权限。在某些情况下,A.png将获得,在某些情况下,A和B将获得完全权限。我找不到原因。请帮我找出原因 String iconFile = themePrefs.getString(info.activityInfo.name, ""); // Icon file = "/data/data/com.android.setti

我使用下面的代码为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。更改为超级用户(su.为此,设备必须根目录),然后您可以更改模式。这是代码。它起作用了。我测试过了。谢谢

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();
    }
}

请准确使用代码,并检查您的设备是否已根目录。它应该有用。从oncreate()调用gainRoot()。如果你有任何困难,请告诉我。谢谢

如果您在运行的上下文中没有读写权限,您将无法在同一上下文中执行chmod。更改为超级用户(su.为此,设备必须根目录),然后您可以更改模式。这是代码。它起作用了。我测试过了。谢谢

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();
    }
}

请准确使用代码,并检查您的设备是否已根目录。它应该有用。从oncreate()调用gainRoot()。如果你有任何困难,请告诉我。谢谢

即使在更改之后,我也得到了同样的例外。请参阅日志Bef-ico:/data/data/my.pack/app/A.png Bef-ico.canExecute():false Bef-ico.canRead():false Bef-ico.canWrite():false Bef-ico.exists():执行代码ico:/data/data/my.pack/app/A.png-ico.canExecute():false-ico.canRead():false-ico.canWrite():false-ico.exists():true java.io.FileNotFoundException:/data/data/com.android.settings/app_themes/settings.png:open failed:EACCES(权限被拒绝)是您测试的根设备吗?在我的代码中,我给出的路径是/dev/A.png。请将其更改为/data/A.png或其所在路径。我想你会这么做的。但是提到它只是为了确认我保存图像的路径是“/data/data/com.android.settings/MyApp/A.png”如何将设备设置为根设备您正在测试的设备是什么?正在获取超级用户权限。每个设备都有一个单独的方法来实现这一点。通常程序员这样做是为了拥有超级用户权限。如果你愿意,我会指引你。但那将由你自己承担风险。即使在改变之后,我也得到了同样的例外。请参阅日志Bef-ico:/data/data/my.pack/app/A.png Bef-ico.canExecute():false Bef-ico.canRead():false Bef-ico.canWrite():false Bef-ico.exists():执行代码ico:/data/data/my.pack/app/A.png-ico.canExecute():false-ico.canRead():false-ico.canWrite():false-ico.exists():true java.io.FileNotFoundException:/data/data/com.android.settings/app_themes/settings.png:open failed:EACCES(权限被拒绝)是您测试的根设备吗?在我的代码中,我给出的路径是/dev/A.png。请将其更改为/data/A.png或其所在路径。我想你会这么做的。但是提到它只是为了确认我保存图像的路径是“/data/data/com.android.settings/MyApp/A.png”如何将设备设置为根设备您正在测试的设备是什么?正在获取超级用户权限。每个设备都有一个单独的方法来实现这一点。通常程序员这样做是为了拥有超级用户权限。如果你愿意,我会指引你。但这将由你自己承担风险。