简体   繁体   English

在Android 4.2中访问/ sdcard

[英]Access /sdcard in Android 4.2

I am developing an app that needs direct access to the root folder of the sdcard, however it seems in Android 4.2, the standard /sdcard directory now points to an "emulated" sdcard specific to the user running the app. 我正在开发一个需要直接访问sdcard根文件夹的应用程序,但是在Android 4.2中,似乎标准的/ sdcard目录现在指向特定于运行该应用程序的用户的“模拟” sdcard。 This is not good, as my app requires access to a file that is stored on the top level of the sdcard. 这不好,因为我的应用程序需要访问存储在sdcard顶层的文件。 Does anyone know how to directly access the sdcard in Android 4.2? 有谁知道如何在Android 4.2中直接访问sdcard?

Can you use the storage directory as a File type? 您可以将存储目录用作文件类型吗? ( java.io.File ) java.io.File

If so, you can get the external storage (Typically SD card, but will be main storage on phones with no SD card) by using code such as this in your method: 如果是这样,您可以通过在您的方法中使用以下代码来获取外部存储(通常为SD卡,但在没有SD卡的电话上将成为主存储):

File path = Environment.getExternalStorageDirectory();

Additionally, access of storage requires READ_EXTERNAL_STORAGE permission in your Android Manifest - with WRITE_EXTERNAL_STORAGE being needed if any data is modified. 此外,要访问存储,您需要在Android Manifest中获得READ_EXTERNAL_STORAGE权限-如果修改了任何数据,则需要WRITE_EXTERNAL_STORAGE Here are these permissions as they would appear in the manifest: 这些权限将出现在清单中:

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

Sources: 资料来源:

http://developer.android.com/reference/android/os/Environment.html http://developer.android.com/reference/android/Manifest.permission.html http://developer.android.com/reference/android/os/Environment.html http://developer.android.com/reference/android/Manifest.permission.html

Perhaps I am misunderstanding your question, however, with my Nexus Galaxy running Android 4.2.1 I can access my sd card using Environment.getExternalStorageDirectory() . 但是,也许我会误解您的问题,但是在运行Android 4.2.1的Nexus Galaxy上,我可以使用Environment.getExternalStorageDirectory()访问我的SD卡。 The returned directory is /storage/emulated/0 , but the content is that of /sdcard . 返回的目录为/storage/emulated/0 ,但内容为/sdcard

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

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