首页 > 解决方案 > UserDefinedFileAttributeView 视图返回 null ,无法设置属性

问题描述

我正在尝试在 android 设备上标记文件,以便稍后根据用户定义的标签(例如 Category 、 Description 、 Owner )显示在列表中。我已经为 UserDefinedFileAttributes 使用了广泛推荐的 java NIO 代码

找到文件路径并报告为有效,但是

final UserDefinedFileAttributeView view = getFileAttributeView (path, 
UserDefinedFileAttributeView.class); 

总是返回 'view' = null

我错过了什么?

也许并非所有平台/设备都支持 UserDefinedFileAttributeView?我已经在 Nexus 6 (API 28) 的 Android Studio 模拟器上尝试过这段代码

String MEDIA_PATH = Environment.getExternalStorageDirectory().getPath()
                   + File.separator + "myEMR" + File.separator + 
                    "Imported_Documents" ;
String uNewFileName = "MyFile";

public void setLocalFileAttributes() throws Exception {

    Path path = Paths.get(MEDIA_PATH, uNewFileName);

    final UserDefinedFileAttributeView view = getFileAttributeView(path, 
          UserDefinedFileAttributeView.class);

    // The file attribute
    final String category = "Category";
    final String value = "UnCategorised";


  final byte[] bytes = value.getBytes("UTF-8");
        final ByteBuffer writeBuffer = 
        ByteBuffer.allocate(bytes.length);
        writeBuffer.put(bytes);
        writeBuffer.flip();
        view.write(category, writeBuffer);

}

标签: javaandroidmetadataniouser-defined

解决方案


推荐阅读