我正在编写一个应用程序,需要“扫描”手机内存以查找具有特定扩展名的文件(现在使用txt进行测试)。我有一个问题,了解Android文件系统实际上是如何工作的 - 我发现如何访问内部和外部SD卡,我可以使用文件浏览器从根文件夹打开它。我无法从应用程序访问这些文件夹。
这是我的代码:
private void findTXT(File directory) {
directory.setReadable(true); //found this advice - doesnt help
if(directory.listFiles() != null) { //if not empty
for (File i : directory.listFiles()) { //iterate trough all files
try {
if (i.isDirectory()) //if it is directory
findTXT(i); //recurse
else if (MimeTypeMap.getFileExtensionFromUrl(i.toURI().toURL().toString()) == "txt") //check ending of regular file
files.add(i); //store it
}
catch(IOException e) {}
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
TextView text = (TextView) findViewById(R.id.textView);
findTXT(new File("/sdcard/")); //this should be phones built-in memory
findTXT(new File("/mnt/sdcard")); //this is the same folder as above
findTXT(Environment.getExternalStorageDirectory()); //this one too
findTXT(new File("/mnt/ext_sdcard")); //this should be removable sdcard
for (File i : getExternalFilesDirs(null) ) //this should be universal?
findTXT(i);
//list file names on screen
if(files != null) {
for (File i : files)
text.setText(text.getText() + i.getName() + "\n");
}
}
以下是我在清单
中的权限<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
问题是,应用程序没有列出/ sdcard或其他文件夹中的文件 - 它似乎只能列出
/storage/emulated/0/Android/data/*com.mycompany.appname*/files
我对permisions有一些问题,我不明白?因为即使经过几个小时的研究,我也无法理解Android上的存储如何工作......
感谢所有答案:)
答案 0 :(得分:0)
试试这个
File directory = Environment.getExternalStorageDirectory();
List<File> files = getListFiles(directory);
....
private List<File> getListFiles(File parentDir) {
ArrayList<File> inFiles = new ArrayList<File>();
File[] files = parentDir.listFiles();
for (File file : files) {
if (file.isDirectory()) {
inFiles.addAll(getListFiles(file));
} else {
if(file.getName().endsWith(".csv")){
inFiles.add(file);
}
}
}
return inFiles;
}
希望它有所帮助。如果你在API级别23.然后你需要询问运行时permisiion
答案 1 :(得分:0)
给予读取和写入运行时权限,如下所示
if (Build.VERSION.SDK_INT >= 23 &&
ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED &&
ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((BaseActivity) context,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_READ_AND_WRITE_SDK);
} else {
callYourMethod();
}
和
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_READ_AND_WRITE_SDK:
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
callYourMethod();
}
break;
}
}