I am trying to access my android files through my apk and (initially) list them on my terminal. This is the code I use:
File basePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)getAbsolutePath();
File directory = new File(basePath);
System.out.println("" + directory.toString());
File[] directoryContents = directory.listFiles();
I do succeed printing this path: /storage/emulated/0/Download
which actually exists in my android and has some files in it.
When listing files inside that directory using:
for (File file : directoryContents)
{
if (file.isFile())
System.out.println("" + file.getName());
else
continue;
}
I get java.lang.NullPointerException: Attempt to get length of null array
I also included <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
in android's manifest.
My smartphone runs on Android 7.0 if that's of any help.