判断obb文件是否存在

作者:追风剑情 发布于:2015-12-25 17:16 分类:Android

一、配置权限

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

 

标签: Android

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号