Filip123go
Filip123go

Reputation: 395

Cannot reach files inside the root directory of my Android app

I have the code bellow:

String path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/data/data/com.example.stavr.mydiary";
File file=new File(path);
File[] files = file.listFiles();
String arr[]=file.list();
List l=new ArrayList<>();
for(String i:arr){
    if(i.endsWith(". txt")){
        l.add(i);
    }
}

which returns 0 files on my array (NullPointerException to be exact), Iknow that there are files in that directory. If i change the directory to just

Environment.getExternalStorageDirectory().getAbsolutePath()

the code work and it returns all the files. I thing that i have done something wrong with the permissions. Could you please help me, Thank you very much in advance..!!!

Upvotes: 0

Views: 92

Answers (1)

Hanan Rofe Haim
Hanan Rofe Haim

Reputation: 918

It seems you're trying to access your internal app data folder, you can do that by using a context object: String dir = context.getFilesDir().getAbsolutePath();

If you still want to access external storage then you should make sureyou have WRITE_EXTERNAL_STORAGE permission in your AndroidManifest.xml

Upvotes: 1

Related Questions