返回顶部

收藏

Android系统下监听SD卡的插入并读取内容

更多
//自己写一个广播监听函数   
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);//使用完注销广播监听函数

标签:SD卡,监听,Android

收藏

0人收藏

支持

0

反对

0