3

I've been having an issue where I cannot get the OBB file to be mounted on the Huawei P20 device despite it working for all other devices.

The ObbStateChangeListener returns a state of 21 but no details of why I cannot mount the Obb file.

Here is the class of my 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;
}

}

The phone will restart as soon as I access a part of the code which calls this class and attempts to access the Obb file in the getFile() method.

Does anyone else have any similar problems with the phone? Or perhaps any information on the Obb mounting?

SamSB
  • 31
  • 1
  • Did you ever find a solution to this? I have also seen this. My obb is over one GB and it's not encrypted. My thoughts have been something to so with size or encryption. – Darthg8r Jun 07 '19 at 14:30

0 Answers0