If you want directory level selection, you can use ACTION_OPEN_DOCUMENT_TREE intent. Additionally, you can request a special all files access to MediaStore. Files from Google Play Console. More on Android 11 storage changes.
Head to Settings > Storage > Other and you'll have a full list of all the files and folders on your internal storage. (If you'd prefer this file manager be more easily accessible, the Marshmallow File Manager app will add it as an icon to your home screen.)
In order to access the files, the permissions must be given in the manifest file.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Try this:
String path = Environment.getExternalStorageDirectory().toString()+"/Pictures";
Log.d("Files", "Path: " + path);
File directory = new File(path);
File[] files = directory.listFiles();
Log.d("Files", "Size: "+ files.length);
for (int i = 0; i < files.length; i++)
{
Log.d("Files", "FileName:" + files[i].getName());
}
I just discovered that:
new File("/sdcard/").listFiles()
returns null if you do not have:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
set in your AndroidManifest.xml file.
Well, the AssetManager
lists files within the assets
folder that is inside of your APK file. So what you're trying to list in your example above is [apk]/assets/sdcard/Pictures.
If you put some pictures within the assets
folder inside of your application, and they were in the Pictures
directory, you would do mgr.list("/Pictures/")
.
On the other hand, if you have files on the sdcard that are outside of your APK file, in the Pictures
folder, then you would use File
as so:
File file = new File(Environment.getExternalStorageDirectory(), "Pictures");
File[] pictures = file.listFiles();
...
for (...)
{
log.e("FILE:", pictures[i].getAbsolutePath());
}
And relevant links from the docs:
File
Asset Manager
In addition to all the answers above:
If you are on Android 6.0+ (API Level 23+) you have to explicitly ask for permission to access external storage. Simply having
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
in your manifest won't be enough. You also have actively request the permission in your activity:
//check for permission
if(ContextCompat.checkSelfPermission(this,
Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED){
//ask for permission
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, READ_EXTERNAL_STORAGE_PERMISSION_CODE);
}
I recommend reading this: http://developer.android.com/training/permissions/requesting.html#perm-request
Your path
is not within the assets folder. Either you enumerate files within the assets folder by means of AssetManager.list()
or you enumerate files on your SD card by means of File.list()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With