Android的API级别23,如何阅读模拟器文件? [英] Android api level 23, how to read files on emulator?

查看:164
本文介绍了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.

推荐答案

File.listFiles()方法

如果此抽象路径名不表示一个目录,或者发生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);

和<一个异步处理结果href=\"https://developer.android.com/reference/android/app/Activity.html#onRequestPermissionsResult%28int,%20java.lang.String[],%20int[]%29\"相对=nofollow> onRequestPermissionsResult(整型,字符串[],INT [])。

该权限仍需要在清单中定义。
如果你想使用 WRITE_EXTERNAL_STORAG​​E 来代替,只是适应它在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.

这篇关于Android的API级别23,如何阅读模拟器文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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