U盘路径,SD卡路径



/**

*注册广播监听U盘挂载
***/
private void initReceiver() {
      
      broadcastRec = new SDcardBroadcastReceiver();
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_BAD_REMOVAL);
//在IntentFilter中选择你要监听的行为
  intentFilter.addDataScheme("file");
  registerReceiver(broadcastRec, intentFilter);//注册监听函数

}


/*
* U盘卡广播类
* */
@SuppressWarnings("unused")
private class SDcardBroadcastReceiver extends BroadcastReceiver{

@SuppressLint("NewApi") @Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub

if (intent.getAction().equals("android.intent.action.MEDIA_MOUNTED")) {
UpanPath = Environment.getStorageState(new File("/mnt/usb/sda1")); //判断sd卡是否存在,/mnt/usb/sda1为U盘路径(具体看自己设备是sda几
// Toast.makeText(MainActivity.this, "sdCard挂载", 1000).show(); 
  }else {
  //Toast.makeText(MainActivity.this, "sdCard拔下", 1000).show(); 
  }
  }
}


  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

 initReceiver();

}


@Override
    protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();

   if (broadcastRec != null) {
  unregisterReceiver(broadcastRec);//使用完注销广播监听函数
}
    }


最后别忘了AndroidManifest.xml加权限:

   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" >
    </uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
    </uses-permission>
    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" >
    </uses-permission>


2,SD卡

   SDcardMounted = Environment.getExternalStorageState()
      .equals(android.os.Environment.MEDIA_MOUNTED); //判断sd卡是否存在


  public String sdCardPath  = "file:///mnt/sdcard";//sd卡路径(/mnt/sdcard也可试试这个)

/*
 * 注册广播
 * */
private void initReceiver() {
broadcastRec = new SDcardBroadcastReceiver();
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中选择你要监听的行为
  intentFilter.addDataScheme("file");
  registerReceiver(broadcastRec, intentFilter);//注册监听函数
}


/*广播接收器*/

private class SDcardBroadcastReceiver extends BroadcastReceiver{

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub

if (intent.getAction().equals("android.intent.action.MEDIA_MOUNTED")) {
  Toast.makeText(MainActivity.this, "sdCard挂载成功", 1000).show(); 

  }else if (intent.getAction().equals("android.intent.action.ACTION_MEDIA_UNMOUNTED")
  || (intent.getAction().equals("android.intent.action.ACTION_MEDIA_BAD_REMOVAL"))) {
 
  Toast.makeText(MainActivity.this, "sdCard未挂载", 1000).show(); 
  }
  }
}


最后别忘了AndroidManifest.xml加权限:

   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission> 
   <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"></uses-permission>   


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);

       initReceiver();
       SDcardMounted = Environment.getExternalStorageState()
      .equals(android.os.Environment.MEDIA_MOUNTED); //判断sd卡是否存在
    }


    
    @Override
    protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
   
    unregisterReceiver(broadcastRec);//使用完注销广播监听函数
    }

你可能感兴趣的:(U盘,SD卡)