Tomáš Lederer Tomáš Lederer - 3 months ago 29
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) {}
}
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);

TextView text = (TextView) findViewById(R.id.textView);

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?
findTXT(i);

//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

/storage/emulated/0/Android/data/*com.mycompany.appname*/files


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 :)

Answer

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},
                    MY_PERMISSIONS_REQUEST_READ_AND_WRITE_SDK);
        } else {
            callYourMethod();


        }

and

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

    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_READ_AND_WRITE_SDK:

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

                callYourMethod();
            }
            break;


    }
}
Comments