Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access /sdcard in Android 4.2

Tags:

java

android

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. This is not good, as my app requires access to a file that is stored on the top level of the sdcard. Does anyone know how to directly access the sdcard in Android 4.2?

like image 243
Drew Avatar asked Dec 01 '12 17:12

Drew


2 Answers

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(). The returned directory is /storage/emulated/0, but the content is that of /sdcard.

like image 179
Daniele Avatar answered Nov 13 '22 06:11

Daniele


Can you use the storage directory as a File type? (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:

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. 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

like image 44
Alex6642122 Avatar answered Nov 13 '22 04:11

Alex6642122