APK expansion file expPath does not exists

2020-03-31 06:54发布

问题:

I have been trying to convert my App to use an APK Expansion file and for some reason this code below cannot find the path to my APK Expansion file ...

static String[] getAPKExpansionFiles(Context ctx, int mainVersion, int patchVersion) {
    Log.v(TAG, "Utils.getAPKExpansionFiles [27] mainVersion is " + mainVersion + " patchVersion = " + patchVersion );
    String packageName = ctx.getPackageName();
    Log.v(TAG, "Utils.getAPKExpansionFiles [27] packageName is [" + packageName + "]" );
    Vector<String> ret = new Vector<String>();
    if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
        Log.v(TAG, "Utils.getAPKExpansionFiles [32] MEDIA_MOUNTED" );
        // Build the full path to the app's expansion files
        File root = Environment.getExternalStorageDirectory();
        Log.v(TAG, "Utils.getAPKExpansionFiles [35] root = " + root);
        File expPath = new File(root.toString() + EXP_PATH + packageName);
        Log.v(TAG, "Utils.getAPKExpansionFiles [37] expPath " + expPath );

        // Check that expansion file path exists
        if (expPath.exists()) {
                ...
        } else {
            Log.v(TAG, "Utils.getAPKExpansionFiles [60] expPath DOES NOT EXISTS" );
        }
    } else {
        Log.v(TAG, "Utils.getAPKExpansionFiles [57] NOT MEDIA_MOUNTED" );
    }
    String[] retArray = new String[ret.size()];
    ret.toArray(retArray);
    return retArray;
}

... logcat shows this ...

03-20 21:04:24.206: V/GospelofMatthewAudiobook(10965): Utils.getAPKExpansionFiles [37] expPath /mnt/sdcard/Android/obb/com.redcricket.GospelofMatthewAudiobook
03-20 21:04:24.214: V/GospelofMatthewAudiobook(10965): Utils.getAPKExpansionFiles [60] expPath DOES NOT EXISTS

... even though the path does exists and the file is there as demonstrated in this screen shot:

I am pretty certain I do not have a typo and the path exists. So why would expPath.exists() return false.

Thanks for the answer Ted ... but I still get this in the logcat ....

03-20 21:59:48.198: V/GospelofMatthewAudiobook(11988): Utils.getAPKExpansionFiles [37] expPath /mnt/sdcard/Android/obb/com.redcricket.GospelofMatthewAudiobook/main.1.com.redcricket.GospelofMatthewAudiobook.obb
03-20 21:59:48.198: V/GospelofMatthewAudiobook(11988): Utils.getAPKExpansionFiles [60] expPath DOES NOT EXISTS

... could it be that I created the directories with Windows and not avd. What would the adb command be to create the dirs and push the APK expansion zip file on to my phone?

回答1:

You need to name the file itself. Instead of this:

File expPath = new File(root.toString() + EXP_PATH + packageName);

use this:

File expPathDir = new File(root.toString() + EXP_PATH + packageName);
File expPath = new File(expPathDir,
    String.format("main.%d.%s.obb", mainVersion, packageName));

This will work for a main expansion pack. You should have a similar method for accessing a patch that uses the format String.format("patch.%d.%s.obb", patchVersion, packageName)

The full path to the file should be something like

/mnt/sdcard/Android/obb/com.redcricket.GospelofMatthewAudiobook/main.123./mnt/sdcard/Android/obb/com.redcricket.GospelofMatthewAudiobook.obb


标签: android apk