title: Android - How can I read text files from external storage
body: <p>I am relatively new to Android Studio and trying to run code below in order to access and read .txt file and set my textView area accordingly from external storage. You can see detailed explation of the problem below;</p>
  <pre><code>package simple_bluetooth_terminal;
  
  import android.Manifest;
  import android.annotation.SuppressLint;
  import android.app.Activity;
  import android.content.Intent;
  import android.content.pm.PackageManager;
  import android.graphics.Color;
  import android.net.Uri;
  import android.os.Build;
  import android.os.Bundle;
  import android.os.Environment;
  import android.util.Log;
  import android.view.View;
  import android.widget.Button;
  import android.widget.TextView;
  import android.widget.Toast;
  
  import androidx.annotation.NonNull;
  import androidx.annotation.Nullable;
  import androidx.annotation.RequiresApi;
  import androidx.fragment.app.FragmentManager;
  import androidx.appcompat.app.AppCompatActivity;
  import androidx.appcompat.widget.Toolbar;
  
  import com.jjoe64.graphview.GraphView;
  import com.jjoe64.graphview.series.DataPoint;
  import com.jjoe64.graphview.series.LineGraphSeries;
  
  import java.io.BufferedReader;
  import java.io.File;
  import java.io.FileNotFoundException;
  import java.io.FileReader;
  import java.io.IOException;
  
  public class MainActivity extends AppCompatActivity implements FragmentManager.OnBackStackChangedListener {
  
      private static final int PERMISSION_REQUEST_STORAGE = 1000;
      private static final int READ_REQUEST_CODE = 42;
      Button b_load;
      TextView tv_output;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          Toolbar toolbar = findViewById(R.id.toolbar);
  
          if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M &amp;&amp; checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED)
          {
              requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSION_REQUEST_STORAGE);
          }
  
          b_load = (Button) findViewById(R.id.read);
          tv_output = (TextView) findViewById(R.id.textView);
          b_load.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
              performFileSearch();
              }
          });
          setSupportActionBar(toolbar);
          getSupportFragmentManager().addOnBackStackChangedListener(this);
          if (savedInstanceState == null)
              getSupportFragmentManager().beginTransaction().add(R.id.fragment, new DevicesFragment(), &quot;devices&quot;).commit();
          else
              onBackStackChanged();
      }
  
      private String readText (String input) {
          File file = new File (Environment.getExternalStorageDirectory(),input);
          StringBuilder text = new StringBuilder();
          try {
              BufferedReader br = new BufferedReader(new FileReader(file));
  
              String line;
              while ((line=br.readLine())!=null){
                  text.append(line);
                  System.out.println(line);
                  text.append(&quot;\n&quot;);
              }
              br.close();
          }catch (IOException e){
              e.printStackTrace();
              Log.e(&quot;Exception caught&quot;, e.toString());
          }
          return text.toString();
      }
  
    
      private void performFileSearch (){
          Intent intent = new Intent (Intent.ACTION_OPEN_DOCUMENT);
          intent.addCategory(Intent.CATEGORY_OPENABLE);
          intent.setType(&quot;text/*&quot;);
          startActivityForResult(intent, READ_REQUEST_CODE);
      }
  
      @Override
      protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
  
          super.onActivityResult(requestCode, resultCode, data);
          if (requestCode == READ_REQUEST_CODE &amp;&amp; resultCode == Activity.RESULT_OK) {
              if (data != null) {
                  Uri uri = data.getData();
                  String path = uri.getPath();
                  if (path.contains(&quot;emulated&quot;)) {
                      path = path.substring(path.indexOf(&quot;0&quot;) + 1);
                  }
                  path = path.substring(path.indexOf(&quot;:&quot;) + 1);
                  Toast.makeText(this, &quot;&quot; + path, Toast.LENGTH_SHORT).show();
                  tv_output.setTextColor(Color.RED);
                  String s = &quot;abc&quot;;
  
                  tv_output.setText(readText(path));
                  System.out.println(&quot;read loop activated&quot;);
                  System.out.println(s);
                  System.out.println(readText(path));
  
              }
          }
      }
  
      @Override
      public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
          if (requestCode== PERMISSION_REQUEST_STORAGE) {
              if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
              {
                  Toast.makeText(this, &quot;Permission granted!&quot;, Toast.LENGTH_SHORT).show();
                  System.out.println(&quot;Permission Granted&quot;);
              } else{
                  Toast.makeText(this, &quot;Permission not granted!&quot;, Toast.LENGTH_SHORT).show();
                  System.out.println(&quot;Permission Granted&quot;);
                  finish();
              }
          }
      }
  
      @Override
      public void onBackStackChanged() {
          getSupportActionBar().setDisplayHomeAsUpEnabled(getSupportFragmentManager().getBackStackEntryCount()&gt;0);
      }
  
      @Override
      public boolean onSupportNavigateUp() {
          onBackPressed();
          return true;
      }
  
  }
  </code></pre>
  <p>I can access the file and see the directory however string in text file is not set in textView.</p>
  <p>Shortly, performFileSearch() works as expected however readText (String input) doesnt work. LOGCAT shows error for the following lines;</p>
  <ol>
  <li>BufferedReader br = new BufferedReader(new FileReader(file));</li>
  <li>tv_output.setText(readText(path));</li>
  </ol>
  <p>and FileNotFound Exception occurs due to problematic BufferedReader line, as follow;</p>
  <p>java.io.FileNotFoundException: /storage/emulated/0/document/acc=1;doc=encoded=yGpDfmZooF+Ikc2k92OckqR7fUXB5WDXoFvDOrmxSLkLTN2N2r8= (No such file or directory)</p>
  <p>I have already included permissions in AndroidManifest.xml;</p>
  <pre><code>&lt;uses-permission android:name=&quot;android.permission.READ_EXTERNAL_STORAGE&quot;/&gt;
  &lt;uses-permission android:name = &quot;android.permission.WRITE_EXTERNAL_STORAGE&quot;/&gt;
  </code></pre>
  <p>You can check log below, any help would be highly appreciated.</p>
  <pre><code>2020-09-15 16:26:00.442 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err: java.io.FileNotFoundException: /storage/emulated/0/document/acc=1;doc=encoded=yGpDfmZooF+Ikc2k92OckqR7fUXB5WDXoFvDOrmxSLkLTN2N2r8= (No such file or directory)
  2020-09-15 16:26:00.442 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err:     at java.io.FileInputStream.open(Native Method)
  2020-09-15 16:26:00.442 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err:     at java.io.FileInputStream.&lt;init&gt;(FileInputStream.java:146)
  2020-09-15 16:26:00.442 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err:     at java.io.FileReader.&lt;init&gt;(FileReader.java:72)
  2020-09-15 16:26:00.442 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err:     at de.kai_morich.simple_bluetooth_terminal.MainActivity.readText(MainActivity.java:74)
  2020-09-15 16:26:00.442 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err:     at de.kai_morich.simple_bluetooth_terminal.MainActivity.onActivityResult(MainActivity.java:114)
  2020-09-15 16:26:00.443 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err:     at android.app.Activity.dispatchActivityResult(Activity.java:7010)
  2020-09-15 16:26:00.443 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err:     at android.app.ActivityThread.deliverResults(ActivityThread.java:4187)
  2020-09-15 16:26:00.443 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:4234)
  2020-09-15 16:26:00.443 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err:     at android.app.ActivityThread.-wrap20(ActivityThread.java)
  2020-09-15 16:26:00.443 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1584)
  2020-09-15 16:26:00.443 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
  2020-09-15 16:26:00.443 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err:     at android.os.Looper.loop(Looper.java:154)
  2020-09-15 16:26:00.443 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6316)
  2020-09-15 16:26:00.443 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
  2020-09-15 16:26:00.443 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
  2020-09-15 16:26:00.443 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
  2020-09-15 16:26:00.443 24764-24764/de.kai_morich.simple_bluetooth_terminal E/Exception caught: java.io.FileNotFoundException: /storage/emulated/0/document/acc=1;doc=encoded=yGpDfmZooF+Ikc2k92OckqR7fUXB5WDXoFvDOrmxSLkLTN2N2r8= (No such file or directory)
  2020-09-15 16:26:00.444 24764-24764/de.kai_morich.simple_bluetooth_terminal I/System.out: read loop activated
  2020-09-15 16:26:00.444 24764-24764/de.kai_morich.simple_bluetooth_terminal I/System.out: abc
  2020-09-15 16:26:00.447 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err: java.io.FileNotFoundException: /storage/emulated/0/document/acc=1;doc=encoded=yGpDfmZooF+Ikc2k92OckqR7fUXB5WDXoFvDOrmxSLkLTN2N2r8= (No such file or directory)
  2020-09-15 16:26:00.447 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err:     at java.io.FileInputStream.open(Native Method)
  2020-09-15 16:26:00.448 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err:     at java.io.FileInputStream.&lt;init&gt;(FileInputStream.java:146)
  2020-09-15 16:26:00.448 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err:     at java.io.FileReader.&lt;init&gt;(FileReader.java:72)
  2020-09-15 16:26:00.448 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err:     at de.kai_morich.simple_bluetooth_terminal.MainActivity.readText(MainActivity.java:74)
  2020-09-15 16:26:00.448 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err:     at de.kai_morich.simple_bluetooth_terminal.MainActivity.onActivityResult(MainActivity.java:117)
  2020-09-15 16:26:00.448 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err:     at android.app.Activity.dispatchActivityResult(Activity.java:7010)
  2020-09-15 16:26:00.448 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err:     at android.app.ActivityThread.deliverResults(ActivityThread.java:4187)
  2020-09-15 16:26:00.448 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:4234)
  2020-09-15 16:26:00.448 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err:     at android.app.ActivityThread.-wrap20(ActivityThread.java)
  2020-09-15 16:26:00.448 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1584)
  2020-09-15 16:26:00.448 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:102)
  2020-09-15 16:26:00.448 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err:     at android.os.Looper.loop(Looper.java:154)
  2020-09-15 16:26:00.448 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6316)
  2020-09-15 16:26:00.448 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
  2020-09-15 16:26:00.448 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:872)
  2020-09-15 16:26:00.448 24764-24764/de.kai_morich.simple_bluetooth_terminal W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:762)
  2020-09-15 16:26:00.449 24764-24764/de.kai_morich.simple_bluetooth_terminal E/Exception caught: java.io.FileNotFoundException: /storage/emulated/0/document/acc=1;doc=encoded=yGpDfmZooF+Ikc2k92OckqR7fUXB5WDXoFvDOrmxSLkLTN2N2r8= (No such file or directory)
  2020-09-15 16:26:00.472 24764-24787/de.kai_morich.simple_bluetooth_terminal D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000
  2020-09-15 16:26:00.498 24764-24787/de.kai_morich.simple_bluetooth_terminal D/mali_winsys: EGLint new_window_surface(egl_winsys_display*, void*, EGLSurface, EGLConfig, egl_winsys_surface**, egl_color_buffer_format*, EGLBoolean) returns 0x3000
  2020-09-15 16:26:00.544 24764-24764/de.kai_morich.simple_bluetooth_terminal I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@249dda8 time:23517629
  </code></pre>