Tomáš Lederer Tomáš Lederer - 1 month ago 10
Java Question

Android: Cant read files from user storage

i'm writing an app that needs to "scan" phone memory for files with specific extension (now using txt for testing purposes). I have a problem understanding how Android filesystem actually works - I found how to access both internal and external sdcard, i can open it with file explorer from root folder. I cannot access these folders from app though.

Here is my code:

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) {}

protected void onCreate(Bundle savedInstanceState) {

TextView text = (TextView) findViewById(;

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?

//list file names on screen
if(files != null) {
for (File i : files)
text.setText(text.getText() + i.getName() + "\n");

Here are my permissions in manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

The problem is, that the app doesnt list files in the /sdcard or other folders - it seems to be able to list only


Am I having some problem with permisions that I dont understand? Because even after few hours of research, I am no closer to understand how storage on Android works...

Thanks for all answers :)


give run time permission for read and write as below

  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},
        } else {



public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    switch (requestCode) {

            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {