访问存储在 Android Q 的内部存储器中的数据库
Accessing Database stored in Internal Storage of Android Q
我想访问存储在内部存储器中的数据库。我正在使用以下代码来执行此操作。
db_connection_string = "URI=file:" + GetAndroidInternalFilesDir() + "/employee.db";
Debug.Log("db_connection_string" + db_connection_string);
db_connection = new SqliteConnection(db_connection_string);
以下是我的 GetAndroidInternalFilesDir
函数。
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 "";
}
上面的代码在
更新:
我已经使用以下代码触发范围存储的权限,但仍然显示零成功。
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);
}
如果您想在列出的目录中搜索(不在您的应用范围内),那么您需要 MANAGE_EXTERNAL_STORAGE
权限。 HERE
中的一些文档
我想访问存储在内部存储器中的数据库。我正在使用以下代码来执行此操作。
db_connection_string = "URI=file:" + GetAndroidInternalFilesDir() + "/employee.db";
Debug.Log("db_connection_string" + db_connection_string);
db_connection = new SqliteConnection(db_connection_string);
以下是我的 GetAndroidInternalFilesDir
函数。
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 "";
}
上面的代码在 更新: 我已经使用以下代码触发范围存储的权限,但仍然显示零成功。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);
}
如果您想在列出的目录中搜索(不在您的应用范围内),那么您需要 MANAGE_EXTERNAL_STORAGE
权限。 HERE