I have an application
that downloads APK's
to the download folder. Once I started supporting SDK 30 and tried the application on Android 10 and 11, I got an error.
downloadRequest = new DownloadManager.Request(uri)
.setTitle(downloadKey)
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, downloadKey)
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
When the download is complete, I have the File and I need to get the package name from it.
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath(), downloadApp.getDownloadKey());
getPackageNameForAPK(file.getPath(), context);
private static String getPackageNameForAPK(final String archiveFilePath, Context context) {
PackageInfo info = context.getPackageManager().getPackageArchiveInfo(archiveFilePath, 0);
return info.packageName;
}
And it crashes on line return info.packageName;
with error:
Caused by: java.lang.NullPointerException: Attempt to read from field 'java.lang.String android.content.pm.PackageInfo.packageName' on a null object reference
I have added all permissions I could think of to manifest and both manually and programmatically enabled all permissions for the application:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="28" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
<uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES"
tools:ignore="QueryAllPackagesPermission" />
I do not think the issue is with permissions since I can get the file size and create a new File from filepath etc.
I assumed it had something to do with Android 11's new requirements about storage but since it is saved to Downloads folder and I have access to the file I do not think it should prevent me from getting the packagename either.