简体   繁体   English

在Android上读写外部存储

[英]Read/write external storage on Android

I'm developing an Android application that reads and writes data in a /sdcard/MyFolder directory. 我正在开发一个Android应用程序,该程序可以在/sdcard/MyFolder目录中读写数据。 I get the sdcard path trough the method Environment.getExternalStorageDirectory() . 我通过方法Environment.getExternalStorageDirectory()获得sdcard路径。

I tested the application on a Samsung device where the path returned by the method is /mnt/sdcard on which I append /MyFolder ; 我在三星设备上测试了该应用程序,该方法返回的路径是/mnt/sdcard ,并在其上附加了/MyFolder all works fine and I can r/w files in this folder. 一切正常,我可以在该文件夹中读/写文件。 Now I'm trying to do the same on a Nexus device: I have put my files to read in the /sdcard/MyFolder directory and getExternalStorageDirectory() returns /storage/emulated/0/ as path, on which I append /MyFolder . 现在,我尝试在Nexus设备上执行相同的操作:我将文件读取到/sdcard/MyFolder目录中,并且getExternalStorageDirectory()返回/storage/emulated/0/作为路径,并在其上附加/MyFolder But in this case I cannot find the files to read. 但是在这种情况下,我找不到要读取的文件。

I set all the storage permissions in the manifest. 我在清单中设置了所有存储权限。

Am I doing something wrong? 难道我做错了什么? Is there any particular issue related to an emulated directory? 是否有与模拟目录有关的特定问题?

Thanks 谢谢

In Android 6.0, you have to handle certain permissions at runtime. 在Android 6.0中,您必须在运行时处理某些权限。 Please go through this . 请经历这个 Because READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE are considered as dangerous permissions and has to be handled at runtime. 因为READ_EXTERNAL_STORAGEWRITE_EXTERNAL_STORAGE被视为危险权限,因此必须在运行时进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM