AndroidのWebViewでindexedDBを使っててUncaught SecurityErrorが出る時の対処法

AndroidでWebViewでindexedDBを使っていると

I/chromium: [INFO:CONSOLE(23)] “Uncaught SecurityError: Failed to execute ‘open’ on ‘IDBFactory’: access to the Indexed Database API is denied in this context.”, source: about:blank (23)

こういうエラーがでることがある。

何が原因か調べてもわからなかったけど、なんか怪しそうなのは

webView.loadDataWithBaseURL(null, index, "text/html", "UTF8", null);

最初の引数にnullが入ってるこれだろうとあたりをつけた。

これを

webView.loadDataWithBaseURL("http://localhost", index, "text/html", "UTF8", null);

こう書き換えたらうまくいった。

考えてみれば当たり前で、ドメイン名が無いとローカルに保存した時にファイル名がつけられないから保存もできないということなのだろう。

そう考えるとlocalhostというのは多分良くない名前なので、アプリ固有の名前とかを使うと良いのかもしれない。

webView.loadDataWithBaseURL("http://application.example.com", index, "text/html", "UTF8", null);

みたいな感じで。