Android - Huawei P20 не может смонтировать файл obb - PullRequest
0 голосов
/ 30 января 2019

У меня возникла проблема, из-за которой я не могу получить файл 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;
}

}

Телефон перезагрузится, как только я получу доступ к частикод, который вызывает этот класс и пытается получить доступ к файлу Obb в методе getFile().

У кого-нибудь еще есть подобные проблемы с телефоном?Или, может быть, какая-нибудь информация о креплении Obb?

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...