正文  数据库 > 文件存储/SD卡 >

Android判断SD卡是否已经挂载

提供一个监听方法BroadcastReceiver 设置IntentFilter为:Intent.ACTION_MEDIA_MOUNTEDIntent.ACTION_MEDIA_EJECTIntent.ACTION_MEDIA_REMOVED然后再public void onReceive(Context context, Intent inten......

提供一个监听方法BroadcastReceiver 设置IntentFilter为:
Intent.ACTION_MEDIA_MOUNTED
Intent.ACTION_MEDIA_EJECT
Intent.ACTION_MEDIA_REMOVED 
然后再public void onReceive(Context context, Intent intent) 中实现你的启动逻辑startActivity
 
 
[代码] [Java]代码
 
private final BroadcastReceiver broadcastRec = new BroadcastReceiver()  
 
{
 
  @Override
 
  public void onReceive(Context context, Intent intent) {
 
  if(intent.getAction().equals("android.intent.action.MEDIA_MOUNTED"))//SD  
 
卡已经成功挂载
 
  {
 
  imagepath =  
 
android.os.Environment.getExternalStorageDirectory();//你的SD卡路径
 
  }else  
 
if(intent.getAction().equals("android.intent.action.MEDIA_REMOVED")//各种未挂载状态
 
  ||intent.getAction().equals("android.intent.action.ACTION_MEDIA_UNMOUNTED")
 
  ||intent.getAction().equals("android.intent.action.ACTION_MEDIA_BAD_REMOVAL"))
 
  {
 
  imagepath = android.os.Environment.getDataDirectory();//你的本地路径
 
  }
 
  }
 
  };
 
  //在IntentFilter中选择你要监听的行为
 
  IntentFilter intentFilter = new  
 
IntentFilter(Intent.ACTION_MEDIA_MOUNTED);
 
  intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
 
  intentFilter.addAction(Intent.ACTION_MEDIA_REMOVED);
 
  //intentFilter.addAction(Intent.ACTION_MEDIA_SHARED);
 
  intentFilter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
 
  //intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
 
  //intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
 
  intentFilter.addDataScheme("file");
 
  registerReceiver(broadcastRec, intentFilter);//注册监听函数
 
  unregisterReceiver(broadcastRec);//使用完注销广播监听函数