2014-02-20 146 views
0

我有一個webview可以通過app訪問localStorage的問題。我的index.html通知我的瀏覽器不支持本地存儲(即webview)。如果您有任何建議。請在我的代碼來看看:Android本地存儲

Activity.java

package com.exercise.AndroidHTML; 




import android.app.Activity; 
import android.os.Bundle; 
import android.webkit.WebSettings; 
import android.webkit.WebView; 


public class AndroidHTMLActivity extends Activity { 

    WebView myBrowser; 
; 
    /** Called when the activity is first created. */ 
    @Override 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     myBrowser = (WebView)findViewById(R.id.mybrowser); 

     myBrowser.loadUrl("file:///android_asset/index.html"); 

     WebSettings webSettings = myBrowser.getSettings(); 
     //enable JavaScript in webview 
     webSettings.setJavaScriptEnabled(true); 
     //Enable and setup JS localStorage 
     webSettings.setDomStorageEnabled(true); 
     webSettings.setDatabaseEnabled(true); 

    } 
} 

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
     package="com.exercise.AndroidHTML" 
     android:versionCode="1" 
     android:versionName="1.0"> 
    <uses-sdk android:minSdkVersion="4" /> 

    <application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name=".AndroidHTMLActivity" 
        android:label="@string/app_name"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

    </application> 
</manifest> 

回答

0

您可能必須設置數據庫路徑:

webView.getSettings()。setDatabasePath(「/ data/data /」+ webView.getCo ntext()。getPackageName()+「/ databases /」);

1

setDatabasePath()方法在API級別19被廢棄我建議你使用存儲區域是這樣的:

webView.getSettings().setDomStorageEnabled(true); 
webView.getSettings().setDatabaseEnabled(true); 
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) { 
    webView.getSettings().setDatabasePath("/data/data/" + webView.getContext().getPackageName() + "/databases/"); 
}