【问题标题】:When I use file.listFiles() on root path it always returns null当我在根路径上使用 file.listFiles() 时,它总是返回 null
【发布时间】:2023-03-16 03:28:01
【问题描述】:

(原谅我的英语不好) 我想在 Android 上制作一个文件资源管理器应用程序,但我无法获取根路径上的文件列表。 我使用 Android Studio 3.1.2 进行开发,并创建了一个非常基本的应用程序进行测试。 我已经在清单和代码中获得了读/写存储权限:

AndroidManifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

主活动

ActivityCompat.requestPermissions(MainActivity.this,
            new String[]{Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE},
            readWritePermmisionRequestCode);

我编写了一个简单的方法来将文件从我的应用目录记录到根路径:

private void searchAllSubFiles() {
    if (targetFilePath.isEmpty()) targetFilePath = getApplicationContext().getFilesDir().getAbsolutePath();
    try{
        File file = new File(targetFilePath);

        String msg = "Path:" + targetFilePath;
        msg += ", file name:" + file.getName();
        msg += ", file readable:" + file.canRead();
        msg += ", file writable:" + file.canWrite();
        msg += ", file is directory:" + file.isDirectory();
        msg += ", file can execute:" + file.canExecute();
        msg += ", sub files:" + file.listFiles();
        Log.d(TAG, msg);

        if (targetFilePath.equals("/"))return;

        targetFilePath = file.getParent();
        searchAllSubFiles();
    } catch(Exception e) {
        Log.e(TAG, "Can not read sub files.");
        e.printStackTrace();
    }
}

这是日志:

Path:/data/user/0/com.test.xux.testviewer/files, file name:files, file readable:true, file writable:true, file is directory:true, file can execute:true, sub files:[Ljava.io.File;@70ac326
Path:/data/user/0/com.test.xux.testviewer, file name:com.test.xux.testviewer, file readable:true, file writable:true, file is directory:true, file can execute:true, sub files:[Ljava.io.File;@c6edb67
Path:/data/user/0, file name:0, file readable:false, file writable:false, file is directory:true, file can execute:true, sub files:null
Path:/data/user, file name:user, file readable:false, file writable:false, file is directory:true, file can execute:true, sub files:null
Path:/data, file name:data, file readable:false, file writable:false, file is directory:true, file can execute:true, sub files:null
Path:/, file name:, file readable:false, file writable:false, file is directory:true, file can execute:true, sub files:null

事实上,它在我的应用程序目录中运行良好。但是在路径 /data/user/0 中,文件变得不可读和不可写。我希望我的应用程序能够像另一个应用程序 PerfectViewer 一样读取手机中的所有文件。我该怎么办?

【问题讨论】:

  • 您的意思是要显示外部存储目录或内部应用程序目录?

标签: android


【解决方案1】:

您正在尝试读取非 root 手机中的最终用户无法读取的 android 操作系统级别目录。

从您的错误日志中可以清楚地看到。

这是你应用的目录所以file readable:true, file writable:true

路径:/data/user/0/com.test.xux.testviewer,文件 名称:com.test.xux.testviewer,文件可读:true,文件可写:true, 文件是目录:真,文件可以执行:真,子 文件:[Ljava.io.File;@c6edb67

现在您正在尝试读取根位置上不可读的文件。

所以它在日志中很清楚

 file readable:false, file writable:false

路径:/data/user/0,文件名:0,文件可读:false,文件 可写:false,文件是目录:true,文件可以执行:true,sub 文件:空

这就是它返回 null 的原因。

【讨论】:

  • 感谢您的回答。所以如果我的应用程序在有根机器上运行它可以正常工作吗?我通常在虚拟设备上测试我的应用,Android Studio 能否将其变成有根设备?
  • 这取决于您的开发目的。您正在为只有根设备开发应用程序?
  • 我还发现了另一个权限调用 MANAGE_DOCUMENTS,但是当我将它添加到清单时,它显示“权限仅授予系统应用程序”。这是否意味着如果我的应用程序是系统应用程序,它可以在非 root 设备上运行,就像我的非 root 索尼手机上的应用程序“文件指挥官”一样?
  • 我不确定。我对Perfect Viewer的功能不满意,所以想制作一个阅读本地漫画的应用。
  • 您可以同时读取内部和外部存储。那么你面临的问题是什么
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-08
  • 2018-06-13
  • 1970-01-01
相关资源
最近更新 更多