//自己写一个广播监听函数
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);//使用完注销广播监听函数
阅读(933) | 评论(0) | 转发(0) |