Android 获取外置USB的路径

  需求:机顶盒用户/维护人员插入带有新版本的U盘后,点击设置项中的USB升级条目,进行升级

难点:
        1,判断U盘是否插入
        2.检测U盘指定路径下是否存在对应apk文件
思路
    路径的获取为难点,一旦路径确定了,那么U盘是否插入也就容易了.
    U盘命名方式多样,已知有sd(a,b,c):sda 或 usbhost1 等,且,相同sda文件夹下子文件夹(文件存放的文件夹)因分区不同有sda1,sda4等

sd表示是SATA硬盘.
a表示第一个硬盘
后面的数字是表示硬盘里的分区.
导致无法确定一个路径,只能动态获取

获取的方案有两种,一是file遍历,/mnt文件夹下插拔U盘前后的变化来判断U盘路径,进而遍历此文件夹内的文件;
二是U盘插拔的广播监听,通过intent.getData().getPath()可获取此路径.(缺点是无法区分U盘还是tf卡)

采用二方法,获取到路径后保存到sp中,拨出U盘后将此路径置为 "" ,通过判断此路径是否存在,来判断U盘是否插入.

 IntentFilter filter = new IntentFilter();
      filter.addAction(Intent.ACTION_MEDIA_SHARED);//如果SDCard未安装,并通过USB大容量存储共享返回
      filter.addAction(Intent.ACTION_MEDIA_MOUNTED);//表明sd对象是存在并具有读/写权限
      filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);//SDCard已卸掉,如果SDCard是存在但没有被安装
      filter.addAction(Intent.ACTION_MEDIA_CHECKING);  //表明对象正在磁盘检查
      filter.addAction(Intent.ACTION_MEDIA_EJECT);  //物理的拔出 SDCARD
      filter.addAction(Intent.ACTION_MEDIA_REMOVED);  //完全拔出
      filter.addDataScheme("file"); // 必须要有此行,否则无法收到广播   
      context.registerReceiver(mReceiver, filter);


U盘插入:
intent.getAction() == android.intent.action.MEDIA_UNMOUNTED
intent.getAction() == android.intent.action.MEDIA_CHECKING
intent.getAction() == android.intent.action.MEDIA_MOUNTED
U盘拔出:
intent.getAction() == android.intent.action.MEDIA_EJECT
intent.getAction() == android.intent.action.MEDIA_UNMOUNTED
intent.getAction() == android.intent.action.MEDIA_UNMOUNTED
intent.getAction() == android.intent.action.MEDIA_REMOVED
intent.getAction() == android.intent.action.MEDIA_UNMOUNTED

SD卡插入:
intent.getAction() == android.intent.action.MEDIA_UNMOUNTED
intent.getAction() == android.intent.action.MEDIA_CHECKING
intent.getAction() == android.intent.action.MEDIA_MOUNTED
SD卡拔出:
intent.getAction() == android.intent.action.MEDIA_EJECT
intent.getAction() == android.intent.action.MEDIA_UNMOUNTED
intent.getAction() == android.intent.action.MEDIA_UNMOUNTED
intent.getAction() == android.intent.action.MEDIA_REMOVED


具体广播接收者如下,用法:
usbStatesReceiver = new USBStatesReceiver(context);
usbStatesReceiver.registerReceiver();
usbStatesReceiver.unregisterReceiver();
public class USBStatesReceiver extends BroadcastReceiver {

	public Intent registerReceiver(Context context) {
		IntentFilter filter = new IntentFilter();
		filter.addAction(Intent.ACTION_MEDIA_CHECKING);//表明对象正在磁盘检查
		filter.addAction(Intent.ACTION_MEDIA_MOUNTED);//表明sd对象是存在并具有读/写权限
		filter.addAction(Intent.ACTION_MEDIA_EJECT);//物理的拔出 SDCARD
		filter.addAction(Intent.ACTION_MEDIA_REMOVED);//完全拔出
		filter.addDataScheme("file");
		return context.registerReceiver(this, filter);
	}

	public void unregisterReceiver(Context context) {
		context.unregisterReceiver(this);
	}

	@Override
	public void onReceive(Context context, Intent intent) {
		Editor editor = context.getSharedPreferences(Constants.TAG_KV_CACHE, Context.MODE_PRIVATE).edit();
		if (intent.getAction().equals(Intent.ACTION_MEDIA_MOUNTED) ) {
			editor.putString(Constants.USB_PATH, intent.getData().getPath());
		} else {
			editor.putString(Constants.USB_PATH, "");
		}
		editor.commit();
	};

}






上述提到的方法一如下,通过前后对比file遍历的结果获取U盘路径

package com.example.storageusbtest;

import java.io.File;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

import com.example.stbusbtest.R;

public class StorageMainActivity extends Activity{
	public TextView usbstates;
	public File file;
	public String[] string;
	MyThread thread;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.storage_main);
		usbstates = (TextView)findViewById(R.id.storageusbstates);
		
		file = new File("/sdcard/video/");
		string = file.list();
		thread = new MyThread();
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		File[] rootfile;
		rootfile = File.listRoots();
		for(String str:string){
			System.out.println("------------------"+str);
		}
		super.onStart();
		thread.start();
		
	}

	class MyThread extends Thread{
		public  MyThread(){};

		@Override
		public void run() {
			// TODO Auto-generated method stub
			super.run();
			System.out.println("=====================start test:"+string.length);
        	usbstates.setText("hahaha"); 
			while (true) { 
				String[] string1 = file.list(); 
		           boolean sign = false; 
		           if (string1.length > string.length) { 
		                for (int i = string1.length -1; i >= 0; i--) { 
		                    sign = false; 
		                    for(int j = string.length -1; j >= 0; j--) { 
		                        if(string1[i].equals(string[j])) { 
		                            sign = true; 
		                        } 
		                    } 
		                    if (sign == false) { 
		                    	usbstates.setText("插入盘符:"+string1[i].toString()); 
		                    } 
		                } 
		                string=file.list();//更新roots  
		           } else { 
		                for (int i = string.length - 1;i >= 0; i--) { 
		                    sign = false; 
		                    for(int j = string1.length- 1; j >= 0; j--) { 
		                        if(string1[j].equals(string[i])) { 
		                            sign = true; 
		                        } 
		                    } 
		                    if (sign == false) { 
		                    	usbstates.setText("退出盘符:"+string[i].toString()); 
		                    } 
		                } 
		                string=file.list();//更新roots  
		           } 
		           try { 
		                sleep(1000); 
		           } catch (InterruptedException ex) { 
		           } 
		       } 
			
		}
	}
	
}













你可能感兴趣的:(Android 获取外置USB的路径)