Android开发:android 4.0 取内外置SD卡新特性

来源:互联网 发布:淘宝客入口在哪里 编辑:程序博客网 时间:2024/06/06 02:04

转载自:http://www.xue5.com/Mobile/Android/710363.html


private StorageManager mStorageManager = null; 

    protected String mSDCardPath = null; 
    protected boolean mSDCardMounted = true; 
    protected boolean mSDCard2Mounted = false; 
    protected String mSDCardPath = null; 
    protected String mSDCard2Path = null; 

  
        mStorageManager = (StorageManager)getSystemService(Context.STORAGE_SERVICE); 
        String[] storagePathList = mStorageManager.getVolumePaths(); 
        if (storagePathList != null) { 
            Log.d(TAG, "StorgaeList size: " + storagePathList.length); 
            if (storagePathList.length >= 2) { 
                mSDCardPath = storagePathList[0]; 
                mSDCard2Path = storagePathList[1]; 
            } else if (storagePathList.length == 1){ 
                mSDCardPath = storagePathList[0]; 
            } 
        } 
        
        Log.d(TAG, "SDCard path: " + mSDCardPath); //取出来的/mnt/sdcard 
        Log.d(TAG, "SDCard2 path: " + mSDCard2Path);//取出来的应该是/mnt/sdcard 

        mSDCardMounted = checkSDCardMount(mSDCardPath); 
        mSDCard2Mounted = checkSDCardMount(mSDCard2Path); 
        Log.d(TAG, "SDCard state in onCreate: " + mSDCardMounted); 
        Log.d(TAG, "SDCard2 state in onCreate: " + mSDCard2Mounted); 
        
    //判断sdcard是否挂载上,返回值为true证明挂载上了,否则不存在 
    protected boolean checkSDCardMount(String mountPoint) { 
        if(mountPoint == null){ 
            return false; 
        } 
        String state = null; 
        state = mStorageManager.getVolumeState(mountPoint); 
        return Environment.MEDIA_MOUNTED.equals(state); 
    } 

所说的4.0新特性是指针对android2.3来说的,android2.3判断内外置sdcard将在下篇文章“android 2.3 取内外置SD卡”带过,以稍作完善。 

0 0