码迷,mamicode.com
首页 > 移动开发 > 详细

android 判断手机中是否有SD卡,USB。静态判断

时间:2015-08-29 09:49:10      阅读:188      评论:0      收藏:0      [点我收藏+]

标签:getexternalstoragedi   sdcard   usb   

    public static String getSDPath(Context context) {
        String sd = null;
        StorageManager storageManager = (StorageManager) context
                .getSystemService(Context.STORAGE_SERVICE);
        StorageVolume[] volumes = storageManager.getVolumeList();
        for (int i = 0; i < volumes.length; i++) {
            if (volumes[i].isRemovable() && volumes[i].allowMassStorage()
                    && volumes[i].getDescription(context).contains("SD")) {
                sd = volumes[i].getPath();
            }
        }
        return sd;
    }
对SD卡的判断分静态和动态,动态通过注册广播,网上很多人都在写,这里就不做过多的阐述。对于这种静态的判断我在网上找的都是
<span style="color: rgb(51, 51, 51); font-family: 宋体; font-size: 14px; line-height: 28px; text-indent: 28px;"> Environment.getExternalStorageState().equals(</span><span style="color: rgb(51, 51, 51); font-family: 宋体; font-size: 14px; line-height: 28px; text-indent: 28px;">Environment.MEDIA_MOUNTED);写代码验证之后发现判断不成功。</span>
<span style="color: rgb(51, 51, 51); font-family: 宋体; font-size: 14px; line-height: 28px; text-indent: 28px;">
</span>
<span style="color: rgb(51, 51, 51); font-family: 宋体; font-size: 14px; line-height: 28px; text-indent: 28px;">下面是对USB的判断。</span>
<span style="color: rgb(51, 51, 51); font-family: 宋体; font-size: 14px; line-height: 28px; text-indent: 28px;"><span style="white-space:pre">	</span></span><pre name="code" class="java">	 public static boolean USBExist(Context context) {
	        boolean ret = false;
	        StorageManager storageManager = (StorageManager) context
	                .getSystemService(Context.STORAGE_SERVICE);

	        if (storageManager == null) {
	            Log.e(TAG, "Invalid reference to StorageManager received.");
	            return ret;
	        }

	        try {
	            if (storageManager.getVolumeState(getUSBPath(context)).equals(
	                    android.os.Environment.MEDIA_MOUNTED)) {
	                ret = true;
	            }
	        } catch (Exception e) {
	            Log.e(TAG, e.toString());
	        }

	        return ret;
	    }
	    public static String getUSBPath(Context context) {
	        String usb = null;
	        StorageManager storageManager = (StorageManager) context
	                .getSystemService(Context.STORAGE_SERVICE);
	        StorageVolume[] volumes = storageManager.getVolumeList();

	        for (int i = 0; i < volumes.length; i++) {

	            if (volumes[i].isRemovable() && volumes[i].allowMassStorage()
	                    && volumes[i].getDescription(context).contains("USB")) {
	                usb = volumes[i].getPath();
	            }
	        }
	        return usb;
	    }


    
        

版权声明:本文为博主原创文章,未经博主允许不得转载。

android 判断手机中是否有SD卡,USB。静态判断

标签:getexternalstoragedi   sdcard   usb   

原文地址:http://blog.csdn.net/lisineng/article/details/48084737

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!