【发布时间】: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