无法在android 6.0中找到sdcard文件(不是硬代码) [英] Can not locate the sdcard file(not hard code) in android 6.0

查看:158
本文介绍了无法在android 6.0中找到sdcard文件(不是硬代码)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自android 6.0起,sdcard路径不再是"/storage/sdcard1/","/storage/sdcard-ext/"或其他名称.

Since android 6.0, the sdcard path is no longer "/storage/sdcard1/", "/storage/sdcard-ext/" or something.

路径取决于电话.如果我使用Nexus 5x AVD,则路径为"/storage/1D15-3A1B/".当我使用Nexus 6p AVD时,路径为"/storage/4679-1802/".那么如何动态地在程序中写入sdcard路径以在外部sdcard中定位文件?

The path depends on the phone instead. If I use Nexus 5x AVD, the path is "/storage/1D15-3A1B/". When I use Nexus 6p AVD, the path is "/storage/4679-1802/". So how can I dynamically write the sdcard path in program to locate the file in external sdcard?

谢谢!

推荐答案

我正在尝试许多方法,包括:

I was trying many approaches, including:

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
Environment.getExternalStorageDirectory()

我的应用程序仍然看不到DCIM/相机中的图片文件.我可以看到目录名称,但是这些目录中的任何一个上的listFiles()都返回null. 最后,令人毛骨悚然的是,我发现清单中没有包含魔术线android.permission.WRITE_EXTERNAL_STORAGE. 添加权限后,它就像是一种魅力.

Still my picture files in DCIM/Camera were not visible to my app. I could see the directory names, but listFiles() on any of these directories returned null. Finally, and sheepishly, I found that my Manifest did not contain the magic line android.permission.WRITE_EXTERNAL_STORAGE. Once I added the permission, it worked like a charm.

故事的寓意:在这种情况下,Android不会警告/破坏/引发异常.当它失败时,它会默默地失败!

Moral of the story: Android does not warn/break/throw exception under this condition. When it fails, it fails silently!

这篇关于无法在android 6.0中找到sdcard文件(不是硬代码)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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