我一直遇到一个问题,尽管它可以在所有其他设备上运行,但我无法将OBB文件挂载在Huawei P20设备上。
ObbStateChangeListener返回的状态为21,但没有为什么我无法挂载Obb文件的详细信息。
这是我的ObbReader的类:
public class ObbReader {
public static final long fileSize = 167878705l;
public static final int version = 44;
public static final String imagesFolder = "/data/";
public static final String ACTION_MOUNT = "com.package.MOUNT";
private static final String EXP_PATH = "/Android/obb/";
private static final String TAG = "ObbReader";
private static ObbReader mInstance = null;
private OnObbStateChangeListener mListener = new OnObbStateChangeListener() {
@Override
public void onObbStateChange(String path, int state) {
super.onObbStateChange(path, state);
if (state == MOUNTED) {
Intent intent = new Intent();
intent.setAction(ACTION_MOUNT);
LocalBroadcastManager.getInstance(ctx).sendBroadcast(intent);
}
}
};
private Context ctx;
private ObbReader(Context ctx) {
this.ctx = ctx.getApplicationContext();
}
public static ObbReader getInstance(Context ctx) {
if (mInstance == null) {
mInstance = new ObbReader(ctx);
}
return mInstance;
}
public static boolean expansionFilesDelivered(Context ctx) {
String fileName = Helpers.getExpansionAPKFileName(ctx, true, version);
if (!Helpers.doesFileExist(ctx, fileName, fileSize, false))
return false;
return true;
}
private static Bitmap loadBitmap(String path, int reqWidth, int reqHeight, boolean compressed) {
InputStream fileStream = null;
Bitmap bmp = null;
try {
fileStream = new FileInputStream(path);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
options.inPurgeable = true;
options.inScaled = true;
options.inJustDecodeBounds = true;
if (compressed) {
options.inPreferredConfig = Bitmap.Config.RGB_565;
}
BitmapFactory.decodeStream(fileStream, null, options);
fileStream.close();
fileStream = new FileInputStream(path);
float scaleFactor = 1.f / Math.min((float) reqWidth / options.outWidth, (float) reqHeight / options.outHeight);
options.inSampleSize = (int) Math.floor(scaleFactor);
options.inJustDecodeBounds = false;
bmp = BitmapFactory.decodeStream(fileStream, null, options);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fileStream != null) {
try {
fileStream.close();
} catch (Exception e) {
}
}
}
return bmp;
}
private static Bitmap getFileFromPath(String folder, String fileName, int width, int height, boolean compressed) {
Bitmap bmp = loadBitmap(folder + fileName + ".jpg", width, height, compressed);
if (bmp == null) {
bmp = loadBitmap(folder + fileName + ".png", width, height, compressed);
}
return bmp;
}
public void mount() {
final StorageManager storageManager = (StorageManager) ctx.getSystemService(Context.STORAGE_SERVICE);
String packageName = ctx.getPackageName();
File root = Environment.getExternalStorageDirectory();
File expPath = new File(root.toString() + EXP_PATH + packageName);
String strMainPath = expPath + File.separator + "main." + version + "." + packageName + ".obb";
if (strMainPath != null && strMainPath.startsWith("/")) {
strMainPath = strMainPath.substring(1);
}
storageManager.mountObb(strMainPath, "AppName", mListener);
}
public static Bitmap getFile(Context ctx, final String fileName, int width, int height, boolean compressed) {
Bitmap bmp = null;
final StorageManager storageManager = (StorageManager) ctx.getSystemService(Context.STORAGE_SERVICE);
String packageName = ctx.getPackageName();
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
File root = Environment.getExternalStorageDirectory();
File expPath = new File(root.toString() + EXP_PATH + packageName);
String strMainPath = expPath + File.separator + "main." + version + "." + packageName + ".obb";
if (strMainPath != null && strMainPath.startsWith("/")) {
strMainPath = strMainPath.substring(1);
}
if (storageManager.isObbMounted(strMainPath)) {
Log.i(TAG, "Obb is mounted");
File path = new File(storageManager.getMountedObbPath(strMainPath));
String folderPath = path.getAbsolutePath() + imagesFolder;
bmp = getFileFromPath(folderPath, fileName, width, height, compressed);
} else {
getInstance(ctx).mount();
}
}
Log.i(TAG, "BMP: " + bmp);
if (bmp == null) {
int resId = ctx.getResources().getIdentifier(fileName, "drawable", ctx.getPackageName());
if (resId != -1) {
InputStream fileStream = null;
try {
fileStream = ctx.getResources().openRawResource(resId);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
options.inPurgeable = true;
options.inScaled = true;
if (compressed) {
options.inPreferredConfig = Bitmap.Config.RGB_565;
}
bmp = BitmapFactory.decodeStream(fileStream, null, options);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fileStream != null) {
try {
fileStream.close();
} catch (Exception e) {
}
}
}
}
}
return bmp;
}
}
一旦我访问了调用此类的一部分代码并尝试使用getFile()
方法访问Obb文件,电话就会重新启动。
其他人手机上是否有类似问题?或有关Obb安装的任何信息?