Adding full permission for a image file in android.

前端 未结 1 1661
长发绾君心
长发绾君心 2021-01-16 22:52

I am using the following code for adding full permission for the image files in android. This code is not working in every time. Imagine there are three image file, A.png,B.

1条回答
  •  -上瘾入骨i
    2021-01-16 23:17

    If you don't have read, write permissions from the context you are running, you will not be able to do chmod from the same context. Change to super user(su. For this the device must be rooted) and then you can change the mode. Here is the code. It works. I tested it. Thanks

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

    Use the code exactly and check if your device is rooted. It shhould work. Call gainRoot() from oncreate(). Let me know in case you have any difficulty. Thanks

    0 讨论(0)
提交回复
热议问题