[安卓]在内存卡创建文件夹
来源:大牛直播内部代码
请求权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
代码:
/** * 根据目录创建文件夹 * @param context * @param cacheDir * @return */ public static File getOwnCacheDirectory(Context context, String cacheDir) { File appCacheDir = null; //判断sd卡正常挂载并且拥有权限的时候创建文件 if ( Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) && hasExternalStoragePermission(context)) { appCacheDir = new File(Environment.getExternalStorageDirectory(), cacheDir); Log.i(TAG, "appCacheDir: " + appCacheDir); } if (appCacheDir == null || !appCacheDir.exists() && !appCacheDir.mkdirs()) { appCacheDir = context.getCacheDir(); } return appCacheDir; } /** * 检查是否有权限 * @param context * @return */ private static boolean hasExternalStoragePermission(Context context) { int perm = context.checkCallingOrSelfPermission("android.permission.WRITE_EXTERNAL_STORAGE"); return perm == 0; }
个性签名:做要做好,做到不三不四不如不做。