有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么GetPackageGearChiveInfo返回null

我想使用getPackageGearChiveInfo获取其他apk的资源,但我得到null,我不知道错误在哪里。这是我的密码

   public void loadSkinAsync(String dexPath, final loadSkinCallBack callback) {
        new AsyncTask<String, Void, Resources>() {

            protected void onPreExecute() {
                if (callback != null) {
                    callback.startloadSkin();
                }
            }

            @Override
            protected Resources doInBackground(String... params) {
                try {
                    if (params.length == 1) {

                        String dexPath_tmp = params[0];
                        File file = new File(dexPath_tmp);
                        PackageManager mPm = mContext.getPackageManager();
                        PackageInfo mInfo = mPm.getPackageArchiveInfo(dexPath_tmp, PackageManager.GET_ACTIVITIES);

                        mPackageName = mInfo.packageName;

                        AssetManager assetManager = AssetManager.class.newInstance();
                        Method addAssetPath = assetManager.getClass().getMethod("addAssetPath", String.class);
                        addAssetPath.invoke(assetManager, dexPath_tmp);

                        Resources superRes = mContext.getResources();
                        Resources skinResource = new Resources(assetManager, superRes.getDisplayMetrics(), superRes.getConfiguration());
                        return skinResource;
                    }
                    return null;
                } catch (Exception e) {
                    e.printStackTrace();
                    return null;
                }

            }

            protected void onPostExecute(Resources result) {
                mResources = result;

                if (callback != null) {
                    if (mResources != null) {
                        callback.loadSkinSuccess();
                    } else {
                        callback.loadSkinFail();
                    }
                }
            }

        }.execute(dexPath);
    }

首先我获取PackageManager,然后我使用它获取PackageInfo,但PackageInfo为空


共 (1) 个答案

  1. # 1 楼答案

    如果您正在读取不在应用程序目录中的文件,则可能会错过以下权限:

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />