2012-08-16 3 views
12

Ho un sito Web che utilizza javascript geolocation api e voglio che si apra in una visualizzazione Web. Ho creato queste autorizzazioni nel file manifesto:Impossibile aprire il database di esplorazione WebView per Android

<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"/> 

Nella mia attività, ho anche configurare le impostazioni WebView:

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

E ho anche gestito javascript dialogo geolocalizzazione:

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

Geolocalizzazione sta funzionando, ma non sono in grado di memorizzare la posizione di partenza nel database. Si dovrebbe fare da sé in CachedGeoposition.db, ma quando avvio il WebView, ottengo questo strano errore di SQLite:

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) 

Quando controllo sull'esistenza di file di CachedGeoposition.db in Esplora file, è sempre lì , e le autorizzazioni del db sono impostate su -rw -------.

Mi sono perso qualcosa nelle impostazioni che cosa potrebbe impedire l'apertura corretta del database? Sono nuovo di Android e sto cercando di trovare una soluzione per 2 giorni. Qualsiasi aiuto sarebbe molto apprezzato. Grazie.

+0

controllare questa risposta http://stackoverflow.com/questions/12801690/error-log-sqlite-failed-to-load-from-cachedgeoposition-db – jpotts18

risposta

0

La linea

D/WebKit (22653): database SQLite è riuscito a caricare da /CachedGeoposition.db

è interessante

Prova a impostare il percorso DB appropriato nella WebSettings.setDatabasePath pure, potrebbe essere che questo sia usato come root per il percorso geo db

0

So che questa è una vecchia domanda ma chiunque abbia questo problema potrebbe trovare interessante concentrarsi su questa funzione:

mWebView.getSettings().setGeolocationDatabasePath(getFilesDir().getPath()); 

La geolocalizzazione utilizza database per mantenere posizioni memorizzate nella cache e autorizzazioni tra le sessioni. Questa chiamata imposta la posizione del database.

Invece di impostare un percorso fisso come parametro che si dovrebbe ottenere in modo dinamico chiamando:

getFilesDir().getPath() 
2

Vi posto una soluzione che ha funzionato per me, nella speranza che potrebbe fornire alcune idee su dove l'errore potrebbe essere. Sarebbe anche d'aiuto se potessi fornire dettagli sul dispositivo (emulatore)/OS su cui stai testando.

ho testato su (senza errori):

  • (Emulator) Galaxy Nexus (2.3.3) (dati/data/package.name/file/file di CachedGeoposition.db creato)
  • (Emulatore) Galaxy Nexus (3.0) (dati/dati/nome_pacchetto/file/file CachedGeoposition.db creato)
  • HTC One S (4.1.1) (file data/data/package.name/files/CachedGeoposition.db creato)
  • Galaxy S3 (4.3) (impossibile vedere il file poiché il mio telefono non è rootato e ci sono alcuni bug "run-as" su 4.3)
  • Nexus 7 (4.4.4) - il webview sopra KIT KAT è cambiato un po 'e il file non c'è più, ma non è stato mostrato alcun errore (anche quando si fornisce un percorso di cartella' database 'errato)
  • (Emulator) Galaxy S4 (5.0) (uguale a 4.4.4)

permessi AndroidManifest (praticamente la stessa):

<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"/> 

impostazioni di visualizzazione Web:

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

Web cro Mi cliente (lo stesso):

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

file html test per ottenere la geolocalizzazione:

<!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> 

Inoltre, per un test locale, si potrebbe creato un file contenente il codice HTML in '/ assets/www /index.html' e utilizzare il seguente codice per caricarlo nella WebView:

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

Leggi metodo del file di asset:

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

Non è stato possibile riprodurre l'errore senza fornire un percorso hardcoded errato alla cartella "database".

+0

OH MY GODNESSS !!!! THANKSSSSSSSSSSS !!!! L'unica linea differente è WebChromeClient 'callback.invoke (origin, true, false);' ma funziona amico !!! –

+0

felice mi ha aiutato :) – Spiri

+0

E 'stato richiesto per il mio lavoro e mi ha fatto impazzire per 2 settimane !! Sapevo che doveva essere una cosa sciocca ... Sai esattamente cosa sta facendo la frase 'callback.invoke'? –