gpt4 book ai didi

android - 从 android 应用程序访问外部存储中某个已下载的 .sqlite 数据库

转载 作者:IT老高 更新时间:2023-10-28 23:25:20 25 4
gpt4 key购买 nike

我正在尝试在基于 android WebView 的应用中实现离线 map 功能:

使用 DownloadManager我从我们的网络服务器下载了一个 sqlite 数据库文件(类似 .mbtiles 的结构),其中包含 map 平铺图像(“离线 map ”)并通过

将其存储在 android 外部存储
public void downloadMap() {
downloadManager = (DownloadManager) getActivity().getSystemService(Activity.DOWNLOAD_SERVICE);
DownloadManager.Request r = new DownloadManager.Request(Uri.parse("http://sry.youtoknow.idontwant/map.sqlite"));

r.setDestinationInExternalFilesDir(getContext(), "map", "map.sqlite");

downloadManager.enqueue(r);
}

下载似乎运行良好。我注册了一个 BroadcastReceiver 来接收 DownloadManager.ACTION_DOWNLOAD_COMPLETE 并使用 SharedPreferences 将路径存储到“map.sqlite”。

为了从 JavaScript 访问数据库以使用 WebView 中的平铺图像,我通过检查自定义虚构模式名称 customhttp: 拦截 XHRequests网址中的:

webView.setWebViewClient(new WebViewClient() {
public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
String url = request.getUrl().toString();
if (!url.startsWith("customhttp:")) {
return null;
} else {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
SQLiteDatabase db = SQLiteDatabase.openDatabase(preferences.getString("map_uri", Environment.getExternalStorageDirectory().getPath() + "berlin.sqlite"), null, SQLiteDatabase.OPEN_READONLY);
}
}
});

我的问题是我似乎以错误的方式实现 openDatabase(),因为我在相应的行中收到以下令人不快的错误,但无法弄清楚为什么:

E/SQLiteLog(#####):      (14) cannot open file at line ##### of [##########]
E/SQLiteLog(#####): (14) os_unix.c:#####: (2) open(//file:///storage/emulated/0/Android/data/idontwant.youtoknow.sry/files/map/map.sqlite) -
E/SQLiteDatabase(#####): Failed to open database 'file:///storage/emulated/0/Android/data/idontwant.youtoknow.sry/files/map/map.sqlite'.
E/SQLiteDatabase(#####): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database

[...]

文件存在,没有损坏,并且它的路径似乎是正确的。我能够使用 SQLite 查看应用程序打开文件。

问题:为什么我的代码不起作用;我怎样才能让它工作?

我知道这里至少存在三个与上述错误消息非常相似的问题,但它们不是那么具体、详细和/或不完全符合我的需求。看了这篇文章我也没有解决这个问题:

http://blog.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/ .

非常感谢您的帮助!


更新:

我尝试在外部存储中使用 SQLiteDatabase.openOrCreateDatabase() 创建一个新数据库,以便将其与原始数据库进行比较。它没有被创建 - 相反,我惊讶地遇到了同样的错误消息(无法打开数据库)。

然后我再次尝试打开一个新数据库,这次是在 RAM 中,使用 SQLiteDatabase.openOrCreateDatabase(":memory:", null) 并没有抛出任何错误。

因此我开始认为问题在于对外部存储的访问限制或寻址外部存储时的错误,而不是数据库文件本身。

最佳答案

我终于找到了错误,一个相当发人深省的错误。

我收到 DownloadManager.ACTION_DOWNLOAD_COMPLETE 并将 URI 存储到 SharedPreferences 中下载的数据库中,供以后引用shouldInterceptRequest 方法使用:

...
Cursor c = downloadManager.query(query);
if (c.moveToFirst()) {
if (c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
Editor editor = PreferenceManager.getDefaultSharedPreferences(getContext()).edit();
editor.putString("map_uri", c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)));
editor.commit();
}
}
c.close();

shouldInterceptRequest 然后我调用了

SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getContext());
SQLiteDatabase db = SQLiteDatabase.openDatabase(preferences.getString("map_uri", Environment.getExternalStorageDirectory().getPath() + "map.sqlite"), null, SQLiteDatabase.OPEN_READONLY);

从而将 URI 传递给 openDatabase,而不仅仅是 Path .

我现在 Uri.parse(preferences.getString("map_uri", ...)).getPath() 一切正常。 map 加载。

非常感谢您的帮助!

关于android - 从 android 应用程序访问外部存储中某个已下载的 .sqlite 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39806926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com