File list files in directory

edited March 2018 in Android Mode

Hi. I've tried several code snippets to get the file names of a specific directory, but all gave me a null. What would be the correct way? Thanks in advance.

import java.io.File;
import android.os.Environment;

// String path = Environment.getExternalStorageDirectory().toString()+"/Download";
 String path = "/storage/emulated/legacy/Download"; 

 File f = new File(path); 
 File[] files = f.listFiles();
 printArray(files);

Answers

  • Answer ✓

    @noel===

    snippet::

    (dont forget to ask permissions for WRITE_EXTERNAL_STORAGE)

        import android.os.Environment;
    
        void setup(){
          size(800,1000);
          background(255,0,0);
          File cheminD = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
          println(cheminD);
    
         File[] files = cheminD.listFiles();
         printArray(files);
        }
    
  • edited March 2018

    Thank you once more. I didn't think this would be necessary since I'm not writing anything. But that's it; all code above works now.

    BTW Why giving the read permission (what would seem more obvious to me) gives the error

    Manifest.permission.READ_EXTERNAL_STORAGE cannot be resolved

Sign In or Register to comment.