无法读取存储在本地存储上的.txt文件

时间:2017-06-22 06:52:31

标签: android android-file

我正在尝试阅读存储在我的SD卡上的 file.txt 文件,但我无法在我的应用上阅读。我提供了

  

"android.permission.READ_EXTERNAL_STORAGE"

清单上的权限。不知道哪里出错了。请在下面看我的代码。

//Find the directory for the SD Card using the API
     //*Don't* hardcode "/sdcard"
     File sdcard = Environment.getExternalStorageDirectory();

     //Get the text file
      File file = new File(sdcard,"file.txt");

     //Read text from file
      StringBuilder text = new StringBuilder();

      try {
       BufferedReader br = new BufferedReader(new FileReader(file));
      String line;

       while ((line = br.readLine()) != null) {
        text.append(line);
        text.append('\n');
    }
    br.close();
     }
      catch (IOException e) {
       //You'll need to add proper error handling here
    }

    //Find the view by its id
     TextView tv = (TextView)findViewById(R.id.text_view);

    //Set the text
     tv.setText(text);

0 个答案:

没有答案