FileNotFoundException : AndroidManifest.xml

不羁的心 提交于 2019-12-23 02:53:43

问题


Im currently working on a project that will check if a certain .apk file is only installed on the device. If it is install, a button, when clicked, will open that application. now, my problem is reading the package name of the .apk for i can open in. i have seen a snippet here on how to read it.

String apkPath = Environment.getExternalStorageDirectory()+ "/KiddieJam/Apps" + file_name;
                    System.out.println(apkPath);
                     PackageManager pm = getPackageManager();

                     PackageInfo info = pm.getPackageArchiveInfo(apkPath, 
                                            PackageManager.GET_ACTIVITIES);
                     Log.i("ActivityInfo", "Package name is " + info.packageName);

Im sure its working properly on other that might have used it also, but im having a problem and my logcat says FileNotFoundException : AndroidManifest.xml

W/PackageParser(13839): Unable to read AndroidManifest.xml of /mnt/sdcard/KiddieJam/Apps/Guitar_Hero_6_Apk.apk /PackageParser(13839): java.io.FileNotFoundException: AndroidManifest.xml W/PackageParser(13839): at android.content.res.AssetManager.openXmlAssetNative(Native Method) W/PackageParser(13839): at android.content.res.AssetManager.openXmlBlockAsset(AssetManager.java:486) W/PackageParser(13839): at android.content.res.AssetManager.openXmlResourceParser(AssetManager.java:454) 05-08 02:33:24.966: W/PackageParser(13839): at android.content.pm.PackageParser.parsePackage(PackageParser.java:401) 05-08 02:33:24.966: W/PackageParser(13839): at android.content.pm.PackageManager.getPackageArchiveInfo(PackageManager.java:1889) 05-08 02:33:24.966: W/PackageParser(13839): at .ContentDetails.onCreate(ContentDetails.java:329) 05-08 02:33:24.966: W/PackageParser(13839): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 05-08 02:33:24.966: W/PackageParser(13839): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 05-08 02:33:24.966: W/PackageParser(13839): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 05-08 02:33:24.966: W/PackageParser(13839): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 05-08 02:33:24.966: W/PackageParser(13839): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 05-08 02:33:24.966: W/PackageParser(13839): at android.os.Handler.dispatchMessage(Handler.java:99) 05-08 02:33:24.966: W/PackageParser(13839): at android.os.Looper.loop(Looper.java:123) 05-08 02:33:24.966: W/PackageParser(13839): at android.app.ActivityThread.main(ActivityThread.java:3683) 05-08 02:33:24.966: W/PackageParser(13839): at java.lang.reflect.Method.invokeNative(Native Method) 05-08 02:33:24.966: W/PackageParser(13839): at java.lang.reflect.Method.invoke(Method.java:507) 05-08 02:33:24.966: W/PackageParser(13839): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 05-08 02:33:24.966: W/PackageParser(13839): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 05-08 02:33:24.966: W/PackageParser(13839): at dalvik.system.NativeStart.main(Native Method)

The .apk file resides on the correct path. I just that .apk for testing purposes only. any help will be appreciated. thanks!


回答1:


Maybe, it is due to "external" storage isolation...

In AndroidManifest.xml, include:

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

For example, somewhere in MyApp.java:

String PACKAGE_NAME = MyApp.class.getPackage().getName(); 
String KEY_DIRECTORY_SELECTED = PACKAGE_NAME + ".DIRECTORY_SELECTED";    

SharedPreference prefs = getSharedPreferences(PACKAGE_NAME, Context.MODE_PRIVATE);

String apkPath = prefs.getString(KEY_DIRECTORY_SELECTED,
    Environment.getExternalStorageDirectory().toString()) + "/KiddieJam/Apps/" + file_name;

System.out.println(apkPath.trim());

PackageManager pm = getPackageManager();

PackageInfo info = pm.getPackageArchiveInfo(apkPath.trim(),PackageManager.GET_ACTIVITIES);

Log.i("ActivityInfo", "Package name is " + info.packageName);

On the other hand, I may suspect you have forgotten the file separator to separate the filename: /KiddieJam/Apps. It should be /KiddieJam/Apps/




回答2:


I have tried with your snippet it is working like charm for me,it returns the package name correctly as com.test

String apkPath = Environment.getExternalStorageDirectory()+ "/download/Check/" + "test.apk";
            System.out.println(apkPath);
             PackageManager pm = getPackageManager();

             PackageInfo info = pm.getPackageArchiveInfo(apkPath, 
                                    PackageManager.GET_ACTIVITIES);
             Log.i("call", "Package name is " + info.packageName);


来源:https://stackoverflow.com/questions/23531833/filenotfoundexception-androidmanifest-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!