1

Getting application info from package manager leads to app crash, why this has occurred and how to fix it?

Error code

Fatal Exception: java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:641)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:978)
Caused by android.content.pm.PackageManager$NameNotFoundException: com.mi.android.globalfileexplorer
   at android.app.ApplicationPackageManager.getApplicationInfoAsUser(ApplicationPackageManager.java:428)
   at android.app.ApplicationPackageManager.getApplicationInfo(ApplicationPackageManager.java:417)
 at com.example.mediapicker.ui.FolderListFragment.setMenuItems(FolderListFragment.kt:92)
   at com.example.mediapicker.ui.FolderListFragment.onPrepareOptionsMenu(FolderListFragment.kt:84)
   at androidx.fragment.app.Fragment.performPrepareOptionsMenu(Fragment.java:3112)
   at androidx.fragment.app.FragmentManager.dispatchPrepareOptionsMenu(FragmentManager.java:3212)
   at androidx.fragment.app.Fragment.performPrepareOptionsMenu(Fragment.java:3114)

App Code

 private fun setMenuItems(apps: MutableList<String>, menu: Menu) {
    menu.clear()
    val pm = requireContext().packageManager
    apps.forEachIndexed { index, packageName ->
        val appInfo = pm.getApplicationInfo(
            packageName,
            PackageManager.GET_META_DATA
        )
        menu.add(
            0, index, Menu.NONE, pm.getApplicationLabel(appInfo)
        ).icon = pm.getApplicationIcon(packageName)
        if (menu is MenuBuilder) {
            menu.setOptionalIconsVisible(true)
        }
    }
}

Manifest file

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mediapicker">

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

<application>
    <activity
        android:name=".ui.MediaPickerActivity"
        android:label="@string/title_activity_media_picker"
        android:theme="@style/ef_ImagePickerTheme"/>
</application>

<queries>
    <intent>
        <action android:name="android.intent.action.PICK" />
        <data android:mimeType="video/*, image/*" />
    </intent>
</queries>

I'm running the above code in my application and I'm facing this crash in One Plus devices how to fix this?

Mohan K
  • 464
  • 5
  • 18
  • `Error code Caused by android.... ` Ok. Caused by... But you omitted the error/exception. Please post more complete stack trace. – blackapps Jun 07 '21 at 07:30
  • @blackapps I have updated the error log – Mohan K Jun 07 '21 at 07:54
  • What also is missing is that you dont show us with which values you call that function. And we have no idea where com.mi.android.globalfileexplorer would come from. – blackapps Jun 07 '21 at 10:28
  • `val appInfo = pm.getApplicationInfo( packageName, PackageManager.GET_META_DATA )` from this line it movesto com.mi.android.globalfileexplorer – Mohan K Jun 07 '21 at 10:41
  • @ZeroCool I have the same problem, The image picker not opening on oneplus nord 2 in my app. Have you solved this problem? If yes, then how? – Chirag Prajapati Nov 25 '21 at 02:44

1 Answers1

1

As stated in the documentation

getApplicationInfo Throws PackageManager.NameNotFoundException if a package with the given name cannot be found on the system.

In your case you are looking for com.mi.android.globalfileexplorer which is the Xiaomi file explorer app that is not installed on your OnePlus device.

Handle the exception and avoid adding the item to the menù if thrown.

Jameido
  • 1,344
  • 1
  • 11
  • 21