pis*_*ron 13 android attributes file setattr
有没有办法让我的Android应用程序检索和设置文件的扩展用户属性?有没有办法java.nio.file.Files在Android 上使用?有没有办法使用setfattr,并 getfattr从我的Dalvik应用程序?我知道android使用ext4文件系统,所以我想它应该是可能的.有什么建议?
HHK*_*HHK 12
Android Java库和仿生C库不支持它.所以你必须使用Linux系统调用的本机代码.
以下是一些示例代码,可帮助您入门,在Android 4.2和Android 4.4上进行测试.
XAttrNative.java
package com.appfour.example;
import java.io.IOException;
public class XAttrNative {
static {
System.loadLibrary("xattr");
}
public static native void setxattr(String path, String key, String value) throws IOException;
}
Run Code Online (Sandbox Code Playgroud)
xattr.c
#include <string.h>
#include <jni.h>
#include <asm/unistd.h>
#include <errno.h>
void Java_com_appfour_example_XAttrNative_setxattr(JNIEnv* env, jclass clazz,
jstring path, jstring key, jstring value) {
char* pathChars = (*env)->GetStringUTFChars(env, path, NULL);
char* keyChars = (*env)->GetStringUTFChars(env, key, NULL);
char* valueChars = (*env)->GetStringUTFChars(env, value, NULL);
int res = syscall(__NR_setxattr, pathChars, keyChars, valueChars,
strlen(valueChars), 0);
if (res != 0) {
jclass exClass = (*env)->FindClass(env, "java/io/IOException");
(*env)->ThrowNew(env, exClass, strerror(errno));
}
(*env)->ReleaseStringUTFChars(env, path, pathChars);
(*env)->ReleaseStringUTFChars(env, key, keyChars);
(*env)->ReleaseStringUTFChars(env, value, valueChars);
}
Run Code Online (Sandbox Code Playgroud)
这适用于内部存储,但不适用于(模拟)外部存储,它使用sdcardfs文件系统或其他内核函数来禁用FAT文件系统不支持的功能,如符号链接和扩展属性.可以说他们这样做是因为可以通过将设备连接到PC来访问外部存储,并且用户希望来回复制文件可以保留所有信息.
这样可行:
File dataFile = new File(getFilesDir(),"test");
dataFile.createNewFile();
XAttrNative.setxattr(dataFile.getPath(), "user.testkey", "testvalue");
Run Code Online (Sandbox Code Playgroud)
虽然这会抛出IOException错误消息:"传输端点上不支持操作":
File externalStorageFile = new File(getExternalFilesDir(null),"test");
externalStorageFile.createNewFile();
XAttrNative.setxattr(externalStorageFile.getPath(), "user.testkey", "testvalue");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3125 次 |
| 最近记录: |