Android webview geoposition database failed to open

前端 未结 3 1192
小鲜肉
小鲜肉 2021-01-02 02:56

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



        
3条回答
  •  再見小時候
    2021-01-02 03:28

    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):

    • (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 permissions (pretty much the same):

    
    
    
    
    
    

    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);
        }
    });
    

    Test html file for getting the geolocation:

    
    
    
    
    
    
    
    
    

    Click the button to get your position.

    No location

    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();
    }
    

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

提交回复
热议问题