0

I would like to access the database that is stored in the Internal storage. I'm using the following code to do so.

db_connection_string = "URI=file:" + GetAndroidInternalFilesDir() + "/employee.db";
Debug.Log("db_connection_string" + db_connection_string);
db_connection = new SqliteConnection(db_connection_string);

Following is my GetAndroidInternalFilesDir function.

public static string GetAndroidInternalFilesDir()
{
    string[] potentialDirectories = new string[]
    {
        "/storage/Company",
        "/sdcard/Company",
        "/storage/emulated/0/Company",
        "/mnt/sdcard/Company",
        "/storage/sdcard0/Company",
        "/storage/sdcard1/Company"
    };

    if(Application.platform == RuntimePlatform.Android)
    {
        for(int i = 0; i < potentialDirectories.Length; i++)
        {
            if(Directory.Exists(potentialDirectories[i]))
            {
                return potentialDirectories[i];
            }
        }
    }
    return "";
}

The above code works fine in every device that is <Android10 but it fails with Android 11 and above. The SDK Version is set to 30 in my Unity3D. I have also tried changing it to 29 with no success. How can I fix this?

UPDATE:

I have used the following code to trigger the permission for scoped storage but still, it shows zero success.

void initiate()
{
    AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject packageManager = jc.Call<AndroidJavaObject>("getPackageManager");
    AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("android.provider.Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION");
    AndroidJavaObject launchIntent = packageManager.Call<AndroidJavaObject>("getLaunchIntentForPackage", packageManager);
    launchIntent = jo.Call<AndroidJavaObject>("setData", packageManager);
    jc.Call("startActivity", launchIntent);
}
Mr. Randy Tom
  • 311
  • 5
  • 13
  • Your code makes no sense as if there was a Company directory then why wouldnt you know its path? Who would have created that directory? Further if there was a database file then why would not you know its in path? As who created that file? – blackapps Jun 21 '21 at 07:33
  • `permission for scoped storage` ??? You dont need permission for scoped storage as you are obliged to use it. It's enforced. You have no choice. That code requests for `all files access`. – blackapps Jun 21 '21 at 07:36

1 Answers1

1

if you want to search in listed directories (not in scope of your app) then you need a MANAGE_EXTERNAL_STORAGE permission. some doc in HERE

snachmsm
  • 17,866
  • 3
  • 32
  • 74
  • Ofcourse, but I don't find an option to do so in Unity3D – Mr. Randy Tom Jun 21 '21 at 05:47
  • you have to execute `startActivity(...)` method using `Intent` with `ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION` action. some samples how to in [HERE](https://stackoverflow.com/questions/10069340/how-to-start-an-android-activity-from-a-unity-application), [HERE](https://stackoverflow.com/questions/43074571/sending-data-using-intent-from-unity-app-to-android-app) or [HERE](https://stackoverflow.com/questions/51460822/unity-android-intents) – snachmsm Jun 21 '21 at 05:51
  • I have tried it but still zero lucks :( please read the updated question – Mr. Randy Tom Jun 21 '21 at 06:08
  • where in posted code you are setting `action` and why you are setting `data`? just use `launchIntent = new AndroidJavaObject("android.content.Intent", "android.settings.MANAGE_ALL_FILES_ACCESS_PERMISSION");`, then just call it with `jc.Call("startActivity", launchIntent);` – snachmsm Jun 21 '21 at 06:57