添加完全权限在Android的一个图像文件。 [英] Adding full permission for a image file in android.

查看:121
本文介绍了添加完全权限在Android的一个图像文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下面的code添加全权限在Android上的图像文件。 这code不工作在每一个时间。想象一下,有三种图像文件,A.png,B.png和C.png。在某些条件下所有的文件都会得到充分的权限。在某些情况A.png会在一定条件下获得或A和B将得到充分的权限。我没能找到原因。请帮我看看是什么原因。

 字符串ICONFILE =主题prefs.getString(info.activityInfo.name,); //图标文件=/data/data/com.android.settings/MyApp/A.png;
的System.out.println(FileExecute+ ico.canExecute()); //始终为假
的System.out.println(FILEREAD+ ico.canRead()); //始终为假
的System.out.println(FILEWRITE+ ico.canWrite()); //始终为假
的System.out.println(FILEEXISTS+ ico.exists()); //总是如此
的System.out.println(FileisFile+ ico.isFile()); //总是如此
的System.out.println(FileisDirectory+ ico.isDirectory()); //始终为假

绘制对象D = NULL;
的FileInputStream FIS; //在流
尝试 {
    文件ICO =新的文件(ICONFILE); //创建与路径的文件(因为只有这正与绝对路径)
    尝试 {
         调用Runtime.getRuntime()EXEC(搭配chmod 777+ ICONFILE)。
    }赶上(IOException异常E1){
     // TODO自动生成的catch块
                        e1.printStackTrace();
    }
    的System.out.println(FileExecute+ ico.canExecute()); //有时候越来越真,有时候假
    的System.out.println(FILEREAD+ ico.canRead()); //有时候越来越真,有时候假
    的System.out.println(FILEWRITE+ ico.canWrite()); //有时候越来越真,有时候假
    的System.out.println(FILEEXISTS+ ico.exists()); //总是如此
    的System.out.println(FileisFile+ ico.isFile()); //总是如此
    的System.out.println(FileisDirectory+ ico.isDirectory()); //始终为假
}
赶上(OutOfMemoryError异常E){
    e.printStackTrace();
}赶上(FileNotFoundException异常E){
    e.printStackTrace();
}赶上(IOException异常E){
    e.printStackTrace();
}赶上(NullPointerException异常NE){
    ne.printStackTrace();
}
 

解决方案

如果您不具有读,写从正在运行的环境许可,你将不能够从同一个背景文件模式。更改为超级用户(SU,对于这个设备必须植根),然后你可以改变模式。这里是code。有用。我测试了它。谢谢

 无效gainRoot()
{
    流程chperm;
    尝试 {
        。chperm =调用Runtime.getRuntime()EXEC(素);
          DataOutputStream类OS =
              新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(退出\ n);
              os.flush();

              chperm.waitFor();

    }赶上(IOException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }赶上(InterruptedException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
    }
}
 

使用的code准确,检查设备是否扎根。它shhould工作。呼叫gainRoot()从OnCreate中()。让我知道,如果你有任何困难。谢谢

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.png and C.png. In some condition all the file will get the full permission. In some condition A.png will get and in some conditions get or A and B will get the full permission. I was not able find the reason. Please help me to find the reason.

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

解决方案

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

这篇关于添加完全权限在Android的一个图像文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆