本文实例讲述了Android判断SD卡是否已经挂载的方法。分享给大家供大家参考。具体如下:
提供一个监听方法BroadcastReceiver 设置IntentFilter为:
Intent.ACTION_MEDIA_MOUNTED
Intent.ACTION_MEDIA_EJECT
Intent.ACTION_MEDIA_REMOVED
然后再public void onReceive(Context context, Intent intent) 中实现你的启动逻辑startActivity
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
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); //使用完注销广播监听函数 |
希望本文所述对大家的Android程序设计有所帮助。