使用nbsp-team/Material File Picker遇异常:Android7.0设备/storage/emulated/0空白
Hey there, let's troubleshoot this frustrating issue with the nbsp-team/Material File Picker on Android 7.0 Nougat devices (your friend's Samsung J7/J5). The fact that it works on older Android versions but shows a blank /storage/emulated/0 on 7.0 points to permission or path-adaptation quirks specific to Nougat's storage handling. Here's what you can check and fix:
1. Double-Check Runtime Permissions (Critical for Android 6.0+)
Android 6.0 Marshmallow introduced runtime permissions, and Nougat enforces them more strictly. Even if you've declared storage permissions in your AndroidManifest.xml, you need to explicitly request them from the user at runtime.
- First, confirm your manifest includes this line:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> - Add code to request and verify the permission before launching the file picker:
private static final int STORAGE_PERMISSION_REQUEST = 101; private void launchFilePicker() { // Check if permission is already granted if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { // Request permission from the user ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, STORAGE_PERMISSION_REQUEST); return; } // Permission is granted, initialize and start the picker new MaterialFilePicker() .withActivity(this) .withRequestCode(1000) .withRootPath(Environment.getExternalStorageDirectory().getAbsolutePath()) .withHiddenFiles(true) // Optional: show hidden folders/files .start(); } // Handle the permission request result @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == STORAGE_PERMISSION_REQUEST) { if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { launchFilePicker(); } else { Toast.makeText(this, "Storage permission is required to browse files", Toast.LENGTH_SHORT).show(); } } }
2. Stop Hardcoding the Storage Path
Hardcoding /storage/emulated/0 is risky because some devices (including certain Samsung models) might use slightly different paths for external storage. Instead, use the official Android API to get the correct root path that works across all devices:
Environment.getExternalStorageDirectory().getAbsolutePath()
This method returns the public external storage root directory that's compatible with Android 7.0 and your friend's Samsung devices.
3. Upgrade to the Latest Version of Material File Picker
Older versions of the nbsp-team/Material File Picker might lack proper compatibility with Android 7.0. Update your dependency to the latest stable release. For example, if you're using Gradle:
implementation 'com.nbsp:library:1.8.1' // Always check for the newest version
Newer releases often fix permission handling and path adaptation bugs for newer Android versions.
4. Verify Hidden Files/Folders Setting
Sometimes, the folders you expect to see might be marked as hidden. Try enabling hidden file support in the picker by adding .withHiddenFiles(true) when initializing it, as shown in the code above.
5. Test for Device-Specific Quirks
Samsung devices on Nougat sometimes have custom storage implementations. If the above steps don't work, test the picker on another Android 7.0 device (non-Samsung) to see if the issue is device-specific. If it only happens on Samsung, you could temporarily test using Context.getExternalFilesDir(null) (though this points to your app's private storage, not the public root) to narrow down the problem.
If none of these steps resolve the issue, check the Material File Picker's issue tracker for similar reports—other developers might have faced the same problem with Samsung Nougat devices and shared fixes.
内容的提问来源于stack exchange,提问作者RFA

