File.listfiles Returns Null When It Shouldn't
I am trying to get list of files existing in the Phone Pictures folder (internal storage, no SDCard inserted) using this: File file = Environment.getExternalStoragePublicDirectory
Solution 1:
If you are targeting Android SDK >=23 you need to take permission at run-time.
https://developer.android.com/training/permissions/requesting.html
Solution 2:
It returns null as Environment.getExternalStoragePublicDirectory
will/is supposed to handle files from external storage like SD card..as you are stating no sd card is inserted
Solution 3:
As Abhishek Aryan suggested, I am using Android SDK >=23 so I had to obtain permissions using code:
boolean grantedAll = ContextCompat.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED;
if (!grantedAll)
{
ActivityCompat.requestPermissions(thisActivity,
newString[]{Manifest.permission.READ_EXTERNAL_STORAGE},
REQUEST_PERMISSIONS);
}
Post a Comment for "File.listfiles Returns Null When It Shouldn't"