Android的API级别23,如何阅读模拟器文件?
我想列出它有一个SD卡仿真器的下载文件夹中的所有文件。
现在,当我做到这一点。
I am trying to list all the files in Downloads folder of an emulator which has an SDCard. Now when I do this
File file = Environment.getExternalStorageDirectory();
值文件
是/存储/ 18E2-1B1D /下载
the value of file
is "/storage/18E2-1B1D/Download"
如果我这样做 file.listFiles()
返回空
。
我不明白这是什么意思,18E2-1B1D?
我必须写单独的code的仿真器和设备?
我已经将所有可能的权限,但它不工作。
我还检查了所有的计算器线程。
I do not understand what this means, 18E2-1B1D? Do I have to write seperate code for emulator and devices? I have set all possible permissions but it isn't working. Also I have checked all the stackoverflow threads.
如果此抽象路径名不表示一个目录,或者发生I / O错误,则返回null。
Returns null if this abstract pathname does not denote a directory, or if an I/O error occurs.
最有可能的文件
表示一个目录。也许它只是尚不存在或许可被拒绝,引起I / O错误。
Most likely file
denotes a directory. Maybe it just doesn't exist yet or a permission is denied, causing an I/O error.
18E2-1B1D
仅仅是SD卡的标识符,所以你不应该担心这一点。最终仿真器和设备应该如果你只使用高级API的工作方式相同。
18E2-1B1D
is just an identifier for the SD card, so you shouldn't have to worry about this. In the end emulator and devices should work the same if you're only using high level APIs.
但在Android的API级别23权限模型改变。现在,你需要要求的权限在运行时的。呼叫
But in Android api level 23 the permission model changed. Now you need to request permissions at runtime. The call
checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
应该返回 PackageManager.PERMISSION_GRANTED
。如果没有,你需要调用
should return PackageManager.PERMISSION_GRANTED
. If not, you'll need to call
requestPermissions(new String[] {Manifest.permission.READ_EXTERNAL_STORAGE}, id);
和 onRequestPermissionsResult(整型,字符串[],INT [])。
该权限仍需要在清单中定义。
如果你想使用 WRITE_EXTERNAL_STORAGE
来代替,只是适应它在code为好。
The permission still needs to be defined in the manifest.
If you want to use the WRITE_EXTERNAL_STORAGE
instead, just adapt it in the code as well.