Granting a temporary URI to another app in Android Studio without using XML

93 views Asked by At

I want to grant an URI to a file that can be any extension (.txt, .mp3, .png, ...) to another app. At first I thought my app has "All File Access" permission provided by user then I can do whatever I want.

Sadly I can only access files inside my app, not others. So is there a way to provide a temporary URI for another app programmatically without using any XML? (exclude AndroidManifest.xml)

Edit: Is there a way to grant a temporary URI to a file that can be any extension (.txt, .mp3, .png, ...) to another app using Java code only?

I'm using Unity for Android to build .apk, and I couldn't find a way to properly tell Unity to put my XML to "resource" folder when building the app, so that's why I can't use XML.

I can access AndroidManifest.xml, that's mean I can declare <provider> component. But it also require <meta-data> which holds what file we want to include for ContentProvider to provide URI for other app, and inside <meta-data> require <android:resource> which need another XML file, so that's not an option, and I couldn't find any way to insert that <meta-data> using Java as well.

Here's my code for granting URI permission:

public void GiveURIPermission()
{
// LoggingManager >> just for logging purpose 
//                >> print to Toast as LogToToast 
//                >> print to WindowManager overlay as LogToNoteOverlay
 
// IOHelper >> For managing input and output of the file
//          >> CombinePath(String... paths): for merging the paths together
//          >> USER_DIRECTORY(String const): "/storage/emulated/0/"
 
 
Intent installIntent = new Intent(Intent.ACTION_INSTALL_PACKAGE);
                    LoggingManager.LogToToast(this, String.valueOf(Uri.parse("content:/" + IOHelper.CombinePath(IOHelper.USER_DIRECTORY, "myfile.apk")))); // this print "content://storage/emulated/0/myfile.apk" -> didn't work
                    installIntent.setData(FileProvider.getUriForFile(
                            this,
                            "com.myapp.anapp",
                            new File(IOHelper.CombinePath(IOHelper.USER_DIRECTORY, "myfile.apk"))
                    ));
                    LoggingManager.LogToNoteOverlay(this, installIntent.getDataString()); // this print an IllegalArgumentException "Couldn't find meta-data for provider with authority com.myapp.anapp" -> didn't work
                    installIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
                    startActivity(installIntent);
}
0

There are 0 answers