Android 如何使用JNI访问系统文件

Android 如何使用JNI访问系统文件,android,permissions,android-ndk,sys,Android,Permissions,Android Ndk,Sys,我需要关于如何使用Jni编辑系统的示例代码。 我需要在/sys/class/gpio/gpio41/value位置编辑文件 我试过这些代码,但不起作用 #include <jni.h> #include <string.h> #include <errno.h> #include <unistd.h> #include <fcntl.h> jstring Java_com_example_hellojni_Hell

我需要关于如何使用Jni编辑系统的示例代码。 我需要在/sys/class/gpio/gpio41/value位置编辑文件

我试过这些代码,但不起作用

#include <jni.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>



 jstring
       Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
                                              jobject thiz )
 {

int fd ;
    char gpio_path[30];
    sprintf(gpio_path,"/sys/class/gpio/gpio41/value");



    fd = open(gpio_path, O_RDWR | O_NONBLOCK );

        write(fd, "1", 2);


    close(fd);



return (*env)->NewStringUTF(env, gpio_path);
#包括
#包括
#包括
#包括
#包括
jstring
Java_com_示例_hellojni_hellojni_stringFromJNI(JNIEnv*env,
jobject thiz)
{
int-fd;
字符gpio_路径[30];
sprintf(gpio_路径,“/sys/class/gpio/gpio41/value”);
fd=打开(gpio_路径,O_RDWR | O_非块);
写入(fd,“1”,2);
关闭(fd);
return(*env)->NewStringUTF(env,gpio_路径);

}

一般来说,由于安全问题,系统文件在android中不可写

特定的应用程序可以写,并具有合适的uid,如系统、媒体、图形等


请参阅并查看TestAllFilesInsysarentWritable()方法。

授予文件执行命令的权限

chmod 777/sys/class/gpio/gpio41/value

它起作用了