我目前正在开发一个项目,该项目将检查某个.apk文件是否仅安装在设备上。如果是安装,单击按钮将打开该应用程序。现在,我的问题是读取.apk的包名称,因为我可以打开。我在这里看到了一个如何阅读它的片段。
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);
我确定它在其他可能也使用过它的地方正常工作,但是我遇到了问题,我的logcat说FileNotFoundException:AndroidManifest.xml
W / PackageParser(13839):无法读取/mnt/sdcard/KiddieJam/Apps/Guitar_Hero_6_Apk.apk的AndroidManifest.xml / PackageParser(13839):java.io.FileNotFoundException:AndroidManifest.xml W / PackageParser(13839):在android.content.res.AssetManager.openXmlAssetNative(Native Method) W / PackageParser(13839):在android.content.res.AssetManager.openXmlBlockAsset(AssetManager.java:486) W / PackageParser(13839):在android.content.res.AssetManager.openXmlResourceParser(AssetManager.java:454) 05-08 02:33:24.966:W / PackageParser(13839):在android.content.pm.PackageParser.parsePackage(PackageParser.java:401) 05-08 02:33:24.966:W / PackageParser(13839):在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):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 05-08 02:33:24.966:W / PackageParser(13839):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 05-08 02:33:24.966:W / PackageParser(13839):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 05-08 02:33:24.966:W / PackageParser(13839):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117) 05-08 02:33:24.966:W / PackageParser(13839):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931) 05-08 02:33:24.966:W / PackageParser(13839):在android.os.Handler.dispatchMessage(Handler.java:99) 05-08 02:33:24.966:W / PackageParser(13839):在android.os.Looper.loop(Looper.java:123) 05-08 02:33:24.966:W / PackageParser(13839):在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)
.apk文件位于正确的路径上。我只是.apk仅用于测试目的。 任何帮助将不胜感激。谢谢!
答案 0 :(得分:0)
我尝试使用你的代码片段,它对我来说就像魅力一样,它正确地返回包名称为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);
答案 1 :(得分:0)
也许,这是由于"外部"存储隔离...
在AndroidManifest.xml
中,包括:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
例如,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);
另一方面,我可能怀疑你忘记了文件分隔符来分隔文件名:/KiddieJam/Apps
。它应该是/KiddieJam/Apps/