Check if the SD card is present

Quickly share your Android Code Snippets here...

Check if the SD card is present

Postby MrSnowflake » Tue Dec 16, 2008 4:12 pm

Hi guys,

Sometimes we need to know whether an SD card is present or not. So here is a check to see if an SD card is present. It returns true if the SD card is present and writable, false otherwise.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public static boolean isSdPresent() {
  2.  
  3. return android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
  4.  
  5. }
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
This seems to be working on the emulator. If someone could verify on the G1 we are sure.

Thanks to Avolovoy for pointing me in the right direction!
Last edited by MrSnowflake on Tue Dec 16, 2008 9:56 pm, edited 4 times in total.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top

Postby avolovoy » Tue Dec 16, 2008 9:48 pm

that should work, but i'd use something like that
if (android.os.Environment.MEDIA_MOUNTED.equals("mounted"))
Alexey Volovoy
Bytesharp.NET
User avatar
avolovoy
Experienced Developer
Experienced Developer
 
Posts: 68
Joined: Mon Nov 03, 2008 10:32 pm
Location: kansas

Postby MrSnowflake » Tue Dec 16, 2008 9:50 pm

That obviously is a LOT better .... thanks!

I'll edit it in to the SP
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby avolovoy » Tue Dec 16, 2008 9:53 pm

Hold on .. i think the right way is call getExternalStorageState() on environment.
http://code.google.com/android/referenc ... nment.html
Alexey Volovoy
Bytesharp.NET
User avatar
avolovoy
Experienced Developer
Experienced Developer
 
Posts: 68
Joined: Mon Nov 03, 2008 10:32 pm
Location: kansas

Postby avolovoy » Tue Dec 16, 2008 10:00 pm

if (android.os.Environment.getExternalStorageState().equals(
android.os.Environment.MEDIA_MOUNTED))
Works on the device just fine.
Alexey Volovoy
Bytesharp.NET
User avatar
avolovoy
Experienced Developer
Experienced Developer
 
Posts: 68
Joined: Mon Nov 03, 2008 10:32 pm
Location: kansas

Postby MrSnowflake » Tue Dec 16, 2008 10:00 pm

Heh, I beat you to it :D.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top

Postby qlimax » Mon Mar 08, 2010 7:33 pm

thanks for sharing this :)
¯`·.¸¸.><((((º>¯`·.¸¸. ><((((º>
User avatar
qlimax
Master Developer
Master Developer
 
Posts: 271
Joined: Mon Aug 31, 2009 10:54 am
Location: Swiss

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 9 guests