Android编程之Sdcard相关代码集锦

2019-12-10 19:54:00王振洲

易采站长站为您分析Android编程之Sdcard相关代码集锦,包括Android针对sd卡的存取、检测、相关信息获取等操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了Android编程之Sdcard相关代码。,具体如下:

1. 检测Sdcard是否可用:

 

 
  1. public static boolean sdCardIsAvailable() {   String status = Environment.getExternalStorageState();  
  2. if (!status.equals(Environment.MEDIA_MOUNTED)) {   return false;  
  3. }   return true;  
  4. }  

2. 获得程序在sd卡上的cahce目录:

 

 
  1. private static boolean hasExternalCacheDir() {   return Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO;  
  2. }   /**  
  3. * @param context 上下文   * @return The external cache dir SD卡路径  
  4. */  private static String getExternalCacheDir(Context context) {  
  5. // android 2.2 以后才支持的特性   if (hasExternalCacheDir()) {  
  6. return context.getExternalCacheDir().getPath() + File.separator + "gesture";   }  
  7. // Before Froyo we need to construct the external cache dir ourselves   // 2.2以前我们需要自己构造  
  8. final String cacheDir = "/Android/data/" + context.getPackageName() + "/cache/gesture/";   return Environment.getExternalStorageDirectory().getPath() + cacheDir;