鸟语天空
判断obb文件是否存在
post by:追风剑情 2015-12-25 17:16

一、配置权限

<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;
		}
	}

 

评论:
发表评论:
昵称

邮件地址 (选填)

个人主页 (选填)

内容