listFiles()在Android 6.0模拟器上返回null [英] listFiles() returns null on Android 6.0 emulator

查看:446
本文介绍了listFiles()在Android 6.0模拟器上返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Android 6.0模拟器上从sdcard读取jpeg文件,但文件列表返回null.该示例代码可以在我的手机上运行:

I want to read jpeg files from sdcard on Android 6.0 emulator, but file list returns null. The sample code can work on my phone:

            String sdcard = Environment.getExternalStorageDirectory().toString();
            File sdcard_dir = new File(sdcard);
            if (sdcard_dir.isDirectory()) {
                File[] fileNames = sdcard_dir.listFiles(new FileFilter() {
                    @Override
                    public boolean accept(File pathname) {
                        return pathname.toString().endsWith(".jpg") ? true : false;
                    }
                });
            }

fileNames = null!

fileNames = null!

我可以使用adb shell在模拟器上列出图像文件:

I can use adb shell to list image files on emulator:

该权限已添加到AndroidManifest.xml:

The permission has been added to AndroidManifest.xml:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

我还能做什么?

我必须使用模拟器,因为到目前为止我的设备无法升级到Android 6.0,并且我想测试一些新的API.

I have to use the emulator because my device cannot be upgraded to Android 6.0 so far, and I want to test some new APIs.

推荐答案

Android 6.0引入了运行时权限.除了在清单上声明许可外,还需要在运行时向用户请求许可.

Android 6.0 introduced Runtime permissions. In addition to declaring the permission on your manifest, you need to request the permission from the user at runtime.

更多信息和教程,请参见: http://developer.android.com/training/Permissions/requesting.html

More info and tutorials here: http://developer.android.com/training/permissions/requesting.html

这篇关于listFiles()在Android 6.0模拟器上返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆