android内部外部存储卡路径的获取

很简单,就是获取外部、内部存储卡路径,不废话,具体详见代码及注释:

   public enum StorageType
    {
        ST_Rom_DataDir, // Android Rom 中data目录
        ST_Rom_AppDir, // Android Rom 中data下app可操作目录
        ST_SDCard_RootDir, // 最大的SD卡的根目录
    };

    public static File getStorageDirectory(StorageType st)
    {
        File f = null;

        if (st.equals(StorageType.ST_Rom_DataDir))
        {
            f = Environment.getDataDirectory();
        }
        else if (st.equals(StorageType.ST_Rom_AppDir))
        {
            f = XApplication.getAppContext().getFilesDir();
        }
        else if (st.equals(StorageType.ST_SDCard_RootDir))
        {
            String sysESDir = Environment.getExternalStorageDirectory().getAbsolutePath();
            String maxESDir = getMaxStorageDir();

            if (maxESDir.isEmpty() || sysESDir.equals(maxESDir))
            {
                f = Environment.getExternalStorageDirectory();
            }
            else
            {
                f = new File(maxESDir);
            }
        }

        return f;
    }

   /**
     * 判断手机是否有SD卡。
     * 
     * @return 有SD卡返回true,没有返回false。
     */
    public static boolean hasSDCard()
    {
        // return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState());
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()))
        {
            return true;
        }
        else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(Environment.getExternalStorageState()))
        {
            return false;
        }
        else
        {
            return false;
        }
    }

   /**
     * 获取图片的本地存储路径。
     * 
     * @return 图片的本地存储路径。
     */
    public static String getPath()
    {
        String path = "";
        // 没有sd卡存放到STRom_dataDir下
        if (!hasSDCard())
        {
            File f = getStorageDirectory(StorageType.ST_Rom_DataDir);
            path = f.getAbsolutePath();
        }
        else
        {
            File f = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
            if (!f.exists())
            {
                // 如果没有DCIM目录, 就放到sd卡
                f = Environment.getExternalStorageDirectory();
            }
            path = f.getPath() + "/" + "picture";
        }

        return path;
    }

    /**
     * 检查sdk权限,检查默认照片存储路径是否存在,不在则创建
     * 
     * @return
     */
    public static boolean checkPath()
    {
        String path = getPath();
        File file = new File(path);
        if (!file.exists())
        {
            file.mkdir();
        }
        if (path.lastIndexOf("picture") != -1)
        {
            return true;
        }
        return false;
    }

猜你喜欢

转载自ontheway-lyl.iteye.com/blog/2082234