Dynamically read files from Sdcard

人走茶凉 提交于 2019-12-14 04:02:55

问题


Guys i have a text file in sdcard i need to read that file.

Below is my code to read files:

 File f = new File(Environment.getExternalStorageDirectory()+"/f1.txt");
       fileIS = new FileInputStream(f);
            BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS));
           String readString = new String(); 
           //just reading each line and pass it on the debugger
           while((readString = buf.readLine())!= null){
              textdata.setText(readString);
              Log.d("line: ", readString);
           }
        } catch (FileNotFoundException e) {
           e.printStackTrace();
        } catch (IOException e){
           e.printStackTrace();
        }

But i need to read files from sdcard dynamically.

here i have given it as f1.txt


回答1:


Try this:

File f = new File(Environment.getExternalStorageDirectory().toString() + "/audio");
if (f.isDirectory())
{
    String files[] = f.list();
    for (int i = 0; i < files.length; i++)
    {
        Log.d("", files[i]);
    }
}


来源:https://stackoverflow.com/questions/9516335/dynamically-read-files-from-sdcard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!