0

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.

Richard
  • 1,087
  • 18
  • 52
  • `I do not think the issue is with permissions since I can get the file size and create a new File from filepath etc.` Can your app delete the file? – blackapps Oct 08 '21 at 11:21
  • `android:name="android.permission.MANAGE_EXTERNAL_STORAGE` You requested that in manifest. But it will not have any effect before you add code and let user confirm. You did not mention this 'all files access'. Well not that you need it i think. – blackapps Oct 08 '21 at 11:24
  • If you think that it has anything to do with owner then let your app make a copy of that apk file first. Then try to get package name from copy. – blackapps Oct 08 '21 at 11:36
  • Well as I wrote.. I have manually enabled the permissions given storage permission as well as unkown apps install permission. I have also included them to programatically check and request the permissions if needed. As for making a copy.. I tried that and I still got the same error – Richard Oct 08 '21 at 13:47
  • You mention the Download folder. Why? Now it looks as if it has something to do with that folder what i doubt. Try another folder. – blackapps Oct 08 '21 at 13:49
  • Repeat: can your app delete the file? – blackapps Oct 08 '21 at 13:56
  • Deleting a file does not throw any errors, but it does not succeed, so the app cannot delete the file. Also on Android 11 Downloads folder should be the only folder where you can download if not in your own `Scoped storage space`? – Richard Oct 08 '21 at 14:22
  • Your app should be able to delete that file as i can here. Also you can use DownloadManager to download to every public directory as i can do here. But what does downloading or Download directory have to do with your problem? Download or copy to another directory and then try again. – blackapps Oct 08 '21 at 14:43

0 Answers0