1

Say, a file explorer now used FileProvider to open an apk file, well, use the intent like the following:

intent.setDataAndType(apkFileUri, "application/vnd.android.package-archive");

I have set up intent-filter in the received activity like this:

            <action android:name="android.intent.action.VIEW" />

            <data
                android:mimeType="application/vnd.android.package-archive"
                android:scheme="content" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>

The point is that how could I get the absolute path of this file from the uri? This uri is not like content or file + absolute path but some personal defined paths by the developers. I retrieved the path by uri.getPath() from different explorers and I got following:

/file/storage/emulated/0/myMessage-global-xxhdpi-debug-1.6.0.apk
/external/file/65971

I need the absolute path of apk file so that I can use the method in PackageManager to get PackageInfo of apk file to show the detailed information like permissions needed, version code and e.t.c. So I really need the absolute path of APK file. I searched around and I know I could get the path if the format is image or audio. But I need apk path.

getPackageArchiveInfo(String archiveFilePath, int flag)
  • I do have a workaround method that I can use InputStream to read the file through this uri, say, copy the file into the disk. And then I can tell the copied apk path. But that might not be a good idea if the apk file is huge like 200MB or even larger. – zzj19931010 Sep 20 '19 at 10:18
  • Use your "workaround". Ideally, Google will add options for those `PackageManager` APIs that support `Uri`, `InputStream`, `FileDescriptor`, or something that you can get from a `Uri`. For now, your file copy approach is the only reliable option. – CommonsWare Sep 20 '19 at 10:57
  • Just confirming what commonsware said, I found these URLs: https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/content/pm/PackageParser.java and https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/content/pm/PackageManager.java. These classes are the classes behind `getPackageArchiveInfo` API, and unfortunately, seems that these classes strongly rely on File API, and don't even consider using URIs or Streams to parse the package info... – Alex Rintt Feb 20 '23 at 00:32

0 Answers0