尝试在外部存储目录下获取文件列表会导致没有文件,即使它们存在并且已经设置了读取目录的权限。
给出以下样本:
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
File ed = Environment.getExternalStorageDirectory();
File[] fs = ed.listFiles();
Log.i(ed.getAbsolutePath(), (fs != null ? fs.length : "no") + " files found");
}
}
的AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
应用程序/的build.gradle
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.test"
minSdkVersion 11
targetSdkVersion 23
...
在一个物理设备(Nexus 5,Android 6.0.1)上测试过,这是logcat:
06-15 11:42:17.309 16732-16732 / com.test I // storage / emulated / 0:找不到文件
没有其他警告或错误。
通过 adb shell工具,我可以看到以下子目录及其权限
$ cd /storage/emulated/
$ ls -l
drwxrwx--x root sdcard_rw 1970-03-05 14:05 Alarms
drwxrwx--x root sdcard_rw 2015-09-10 23:44 Android
drwxrwx--x root sdcard_rw 2015-08-11 18:40 BDArenaConnector
drwxrwx--x root sdcard_rw 2015-02-03 14:14 DCIM
drwxrwx--x root sdcard_rw 2016-06-14 10:59 Download
drwxrwx--x root sdcard_rw 2015-09-15 11:48 MOGO_AUDIANCE
drwxrwx--x root sdcard_rw 2015-06-19 11:49 Manga Mobile
drwxrwx--x root sdcard_rw 2015-10-19 21:02 Memes
drwxrwx--x root sdcard_rw 1970-03-05 14:05 Movies
drwxrwx--x root sdcard_rw 2016-05-24 11:01 Music
drwxrwx--x root sdcard_rw 2015-01-12 20:43 Notifications
drwxrwx--x root sdcard_rw 2015-11-24 19:19 PerfectViewer
drwxrwx--x root sdcard_rw 2016-06-01 16:58 Pictures
drwxrwx--x root sdcard_rw 1970-03-05 14:05 Podcasts
drwxrwx--x root sdcard_rw 2015-04-30 10:16 ROMs
drwxrwx--x root sdcard_rw 2015-01-12 20:36 Ringtones
drwxrwx--x root sdcard_rw 2015-08-10 18:57 SKMaps
drwxrwx--x root sdcard_rw 2015-09-15 11:48 ShareSDK
drwxrwx--x root sdcard_rw 2015-08-16 10:40 Telegram
drwxrwx--x root sdcard_rw 2015-05-29 21:36 TuneIn Radio
drwxrwx--x root sdcard_rw 2016-05-27 00:08 WhatsApp
drwxrwx--x root sdcard_rw 2015-05-20 09:21 airdroid
drwxrwx--x root sdcard_rw 2016-05-31 16:59 backups
drwxrwx--x root sdcard_rw 2015-11-10 14:42 bluetooth
drwxrwx--x root sdcard_rw 2015-11-15 19:43 com.facebook.katana
drwxrwx--x root sdcard_rw 2015-08-10 18:44 > com.facebook.orca
drwxrwx--x root sdcard_rw 2015-09-15 11:51 ktplay
drwxrwx--x root sdcard_rw 2015-04-23 20:30 media
drwxrwx--x root sdcard_rw 1970-03-05 14:04 obb
drwxrwx--x root sdcard_rw 2015-02-07 19:11 panoramas
drwxrwx--x root sdcard_rw 2015-06-19 11:53 smart-tools
drwxrwx--x root sdcard_rw 2015-09-28 12:56 wp
我知道这个目录应该可供应用程序读取,因为其他一些应用程序没有任何问题(例如Astro File Manager或PerfectViewer)。
答案 0 :(得分:1)
再次编译时需要运行时权限6.0
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_RUNTIME_PERMISSION = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
checkPremission();
}
void checkPremission() {
//select which permission you want
final String permission = Manifest.permission.WRITE_EXTERNAL_STORAGE;
// if in fragment use getActivity()
if (ContextCompat.checkSelfPermission(ActivityName.this, permission)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(ActivityName.this, permission)) {
} else {
ActivityCompat.requestPermissions(ActivityName.this, new String[]{permission}, REQUEST_CAMERA_PERMISSION);
}
} else {
// you have permission go ahead
File ed = Environment.getExternalStorageDirectory();
File[] fs = ed.listFiles();
Log.i(ed.getAbsolutePath(), (fs != null ? fs.length : "no") + " files found");
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_RUNTIME_PERMISSION:
final int numOfRequest = grantResults.length;
final boolean isGranted = numOfRequest == 1
&& PackageManager.PERMISSION_GRANTED == grantResults[numOfRequest - 1];
if (isGranted) {
// you have permission go ahead
File ed = Environment.getExternalStorageDirectory();
File[] fs = ed.listFiles();
Log.i(ed.getAbsolutePath(), (fs != null ? fs.length : "no") + " files found");
}else{
// you dont have permission show toast
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
}