Get Path from URI & How to get permission from APK via Path?

Path from URI

I asked on the Kodular Community, https://community.kodular.io/t/get-path-from-uri/41516 however I was unable to achieve this, read below to understand the current situation.

I’m creating an app which I would like to not speak about what it is right now, but you have to select an APK. Now when I get the Result URI from the Activity Starter after picking the APK, I get content://com.android.providers.downloads.documents/document/1332. I’m wondering how I can make this into a path that I can get the APK from and upload it to something.

Permission list from APK via Path

This is basically an extension from what’s above, when they select their APK file, I would like it to get the apps permissions using GetPackageArchiveInfo(path, PackageManager.GET_PERMISSIONS), but I don’t know how to return it as a readable list as a string.

I don't believe that it is possible to translate this into a file URI as content URIs are not guaranteed to exist in the file system at all. It's possible there is a way to do this in Android, but it escapes me and looking at StackOverflow might be the best opportunity to find a solution for this.

It seems like what you would want to do is the following:

  1. Open an InputStream to the content using ContentResolver's openInputStream(Uri) method.
  2. Create a ZipInputStream to wrap the input stream.
  3. Locate the ZipEntry for the AndroidManifest.xml entry in the ZipInputStream and read it into a byte array.
  4. Use something like this gist to decompress the manifest back into XML and then parse out the uses-permission tags.
1 Like

Can I have an example?

Rough sketch without error checking etc.:

ZipInputStream zis = new ZipInputStream(form.getContentResolver().openInputStream(uri));
ZipEntry entry;
ByteArrayOutputStream xmldata = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int read;
while((entry = zis.getNextEntry()) != null) {
  if (entry.getName().equals("/AndroidManifest.xml")) {
    while((read = zis.read(buf, 0, 1024)) > 0) {
      xmldata.write(buf, 0, read);
    }
    break;
  }
}
// xmldata.toByteArray() contains the compressed AndroidManfest.xml file
2 Likes

OK, now how do I get the permission list as a readable string and the path of the APK file? Those were my main questions.

Like I said in my previous post, you can use the code in the linked Gist to decompress the XML string. Then just use a regex to parse out the uses-permission tag.

It’s possible you might be able to get a file URI equivalent using this technique (although not guaranteed):

1 Like

I’m going to try putting everything together and see what I get from it and if it works. If it doesn’t Ill come back for help, thanks!

I figured out how to get permissions from APK path, also figured out a new way to pick out APKs.