一、配置权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
二、判断方法
/** * 判断main.versionCode.packageName.obb是否存在 * @return */ public boolean hasMainOBB(){ try { String externalStorageState = Environment.getExternalStorageState(); if(!externalStorageState.equals(Environment.MEDIA_MOUNTED)){ Log.e(TAG, "对扩展SD卡无读写权限 externalStorageState="+externalStorageState); return false; } PackageInfo pi = this.getPackageManager().getPackageInfo(this.getPackageName(), 0); int versionCode = pi.versionCode; String packageName = pi.packageName; String obbPath = "Android/obb"; File externalStorageDirectory = Environment.getExternalStorageDirectory(); String root = externalStorageDirectory.getPath(); String expansionFilePath = String.format("%s/%s/%s", root, obbPath, packageName); String mainObbName = String.format("main.%d.%s.obb", versionCode, packageName); String mainObbFilePath = String.format("%s/%s", expansionFilePath, mainObbName); Log.d(TAG, mainObbFilePath); File obbFile = new File(mainObbFilePath); return obbFile.exists(); } catch (NameNotFoundException e) { Log.e(TAG, e.getLocalizedMessage()); return false; } }