我已经构建了一个应用程序,我应该只浏览设备中的txt文件并阅读它。但是我没有得到有效的解决方案。例如,如果我们使用whatsapp,则可以在不同的文件下浏览不同的文件标题,例如在视频下,您将仅浏览视频;在画廊下,您将仅浏览图像。但是,仅文本文件呢?有什么办法吗?请建议。
最佳答案
try this.
//for example your folder present in sdcard/cts.
File f = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+ "/cts");
for (File wavfile : f.listFiles(new FileExtensionFilter()))
{
String str = wavfile.getName().toString();
}
//create a class and the filter based on your need file extension
class FileExtensionFilter implements FilenameFilter {
public boolean accept(File dir, String name) {
return (name.endsWith(".txt") || name.endsWith(".TXT") );
}
}
thank you.