机器人的WebView geoposition数据库无法打开机器人、无法打开、数据库、geoposition

2023-09-06 01:53:49 作者:情感荒废者

我有使用javascript地理位置API的网站,并希望它在一个web视图中打开。我在清单文件中设置了这些权限:

I have website using javascript geolocation api and want it to open in a webview. I set up these permissions in the manifest file:

<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

在我的活动,我还设置web视图设置:

In my activity, I also set webview settings:

webview.getSettings().setDatabaseEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.getSettings().setGeolocationDatabasePath("/data/data/com.my.app/databases");
webview.getSettings().setGeolocationEnabled(true);

和我也处理JavaScript的地理位置对话框:

And I also handled javascript geolocation dialog:

webview.setWebChromeClient(new WebChromeClient(){
    @Override
    public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
        callback.invoke(origin, true, false);
    }
});

地理位置本身的工作,但我不能够缓存geoposition到数据库中。应该做它用自己变成CachedGeoposition.db,但是当我启动web视图,我得到这个奇怪的SQLite的错误:

Geolocation itself is working, but I am not able to cache geoposition into the database. It should be doing it by itself into CachedGeoposition.db, but when I start the webview, i get this strange SQLite error:

E/SQLiteLog(22653): (14) cannot open file at line 30174 of [00bb9c9ce4]
E/SQLiteLog(22653): (14) os_unix.c:30174: (2) open(/CachedGeoposition.db) -
D/WebKit  (22653): ERROR:
D/WebKit  (22653): SQLite database failed to load from /CachedGeoposition.db
D/WebKit  (22653): Cause - unable to open database file
D/WebKit  (22653):
D/WebKit  (22653): external/webkit/Source/WebCore/platform/sql/SQLiteDatabase.cp
p(71) : bool WebCore::SQLiteDatabase::open(const WTF::String&, bool)

当我检查的文件浏览器存在CachedGeoposition.db文件,它是永远存在的, 和数据库的权限设置为-rw -------。

When I check existence of CachedGeoposition.db file in File Explorer, it is always there, and permissions of the db are set to -rw-------.

我错过了什么在设置这可能导致数据库不能正常打开? 我是新来的Andr​​oid,并试图寻找2天液了。任何帮助将大大AP preciated。谢谢你。

Did I miss something in settings what could cause database not to open correctly? I'm new to Android and trying to find solution for 2 days now. Any help would be greatly appreciated. Thank you.

推荐答案

我会后说,希望它可能会提供在何处的错误可能是一些想法为我工作的解决方案。这也将有助于如果你能提供有关设备(模拟器)/ OS上,您所测试的细节。

I'll post a solution that worked for me in the hope that it might provide some ideas on where the error might be. It would also help if you could provide details about the device(emulator)/os on which you're testing.

我测试了(没有错误):

I tested on (with no errors):

(模拟器)Galaxy Nexus的(2.3.3)(数据/数据​​/ package.name /文件/ CachedGeoposition.db文件创建) (模拟器)Galaxy Nexus的(3.0)(数据/数据​​/ package.name /文件/ CachedGeoposition.db文件创建) 的HTC One S(4.1.1)(数据/数据​​/ package.name /文件/ CachedGeoposition.db文件创建) 的Galaxy S3(4.3)(无法看到,因为我的手机是不是植根文件,也有一些运行为错误的4.3) 的Nexus 7(4.4.4) - 上面奇巧的的WebView已经改变了位, 文件不再存在,但显示没有错误(提供了一个错误的数据库文件夹路径,即使) (模拟器)的Galaxy S4(5.0)(同在4.4.4) (Emulator) Galaxy Nexus (2.3.3) (data/data/package.name/files/CachedGeoposition.db file created) (Emulator) Galaxy Nexus (3.0) (data/data/package.name/files/CachedGeoposition.db file created) HTC One S (4.1.1) (data/data/package.name/files/CachedGeoposition.db file created) Galaxy S3 (4.3) (couldn't see the file since my phone is not rooted and there are some 'run-as' bugs on 4.3) Nexus 7 (4.4.4) - the webview above KIT KAT has changed a bit and the file is no longer there, but no error was shown (even when providing a wrong 'databases' folder path) (Emulator) Galaxy S4 (5.0) (same as on 4.4.4)

AndroidManifest权限(pretty的大致相同):

AndroidManifest permissions (pretty much the same):

<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Web视图设置:

Web view settings:

WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDatabaseEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setGeolocationDatabasePath(getFilesDir().getPath());
webSettings.setGeolocationEnabled(true);

网络铬客户端(下同):

Web chrome client (the same):

webview.setWebChromeClient(new WebChromeClient(){
    @Override
    public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
        callback.invoke(origin, true, false);
    }
});

有关获取地理位置测试HTML文件:

Test html file for getting the geolocation:

<!DOCTYPE html>
<html>
<head>
<script>
var x = document.getElementById("demo");

function getLocation() {
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(showPosition, showError);
    } else {
        x.innerHTML = "Geolocation is not supported by this browser.";
    }
}

function showPosition(position) {
    var latlon = position.coords.latitude + "," + position.coords.longitude;
    document.getElementById("locationHolder").innerHTML = latlon;
}

function showError(error) {
    switch(error.code) {
        case error.PERMISSION_DENIED:
            x.innerHTML = "User denied the request for Geolocation.";
            break;
        case error.POSITION_UNAVAILABLE:
            x.innerHTML = "Location information is unavailable";
            break;
        case error.TIMEOUT:
            x.innerHTML = "The request to get user location timed out.";
            break;
        case error.UNKNOWN_ERROR:
            x.innerHTML = "An unknown error occurred.";
            break;
    }
}
</script>
</head>

<body>

<p id="demo">Click the button to get your position.</p>

<div id="locationHolder">No location</div>
<button onclick="getLocation()">Get position</button>

</body>
</html>

另外,对于本地测试,您可以创建一个包含HTML/assets/www/index.html项下,并使用以下code加载到web视图文件:

Also, for a local test, you could created a file containing the html under '/assets/www/index.html' and use the following code to load it into the webview:

try {
    String html = readAssetFile("www/index.html");
    webview.loadDataWithBaseURL(null, html, "text/html", "UTF-8", null);
} catch (IOException e) {
}

阅读资源文件的方法:

Read asset file method:

private String readAssetFile(String filePath) throws IOException {
    StringBuilder buffer = new StringBuilder();
    InputStream fileInputStream = getAssets().open(filePath);
    BufferedReader bufferReader = new BufferedReader(new InputStreamReader(fileInputStream, "UTF-8"));
    String str;

    while ((str=bufferReader.readLine()) != null) {
        buffer.append(str);
    }
    fileInputStream.close();

    return buffer.toString();
}

我不能没有提供错误的硬codeD路径数据库文件夹中复制您的错误。

I couldn't reproduce your error without providing a wrong hardcoded path to the 'databases' folder.