I want to get a JSON file from my HTML file in the assets folder.
But I get a java.io.FileNotFoundException: No content provider: error in my app.
The DownloadListener returns a URL of blob:file:///bf92b937-be38-43d4-9bda-ad4e1e7b98ae.
But I cannot get data from this URL.
This page is a local HTML file in the assets folder.
How can I read the blob:file:///bf92b937-be38-43d4-9bda-ad4e1e7b98ae URL?
MainActivity
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String TAG = getClass().getSimpleName();
WebView myWebView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowContentAccess(true);
webSettings.setAllowFileAccess(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
webSettings.setAllowFileAccessFromFileURLs(true);
webSettings.setBuiltInZoomControls(true);
myWebView.setMinimumHeight(500);
myWebView.setWebViewClient(new WebViewClient());
myWebView.setWebChromeClient(
new WebChromeClient(){
public boolean onConsoleMessage(ConsoleMessage cm) {
Log.d(TAG,cm.message() + " -- From line " + cm.lineNumber() + " of " + cm.sourceId());
return true;
}
}
);
Log.d(TAG, "test start:");
myWebView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
String METHOD = "setMyWebView:onDownloadStart" ;
Log.d(TAG, "onDownloadStart: " + url);
Log.d(TAG, "contentDisposition:" + contentDisposition + " mimetype:" + mimetype + " contentlength:" + contentLength);
ContentResolver resolver = getContentResolver();
Uri downloadUri = Uri.parse(url);
try (InputStream inputStream = resolver.openInputStream(downloadUri)) {
// Parse the JSON data
byte[] bytes = new byte[inputStream.available()];
inputStream.read(bytes);
String jsonString = new String(bytes);
Log.d(TAG, "JSON: " + jsonString);
} catch (Exception e) {
Log.e(TAG, "onDownloadStart: ", e);
}
}
});
myWebView.loadUrl("file:///android_asset/test/test.html");
}
}
asset/test/test.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<script>
jsondata = JSON.parse('{"key":"A","data":"123"}');
const textjson = JSON.stringify(jsondata);
console.log(textjson);
const jsonname = "TEST.json";
const downLoadLink = document.createElement("a");
downLoadLink.download = jsonname;
downLoadLink.href = URL.createObjectURL(new Blob([textjson], {type: "text.plain"}));
downLoadLink.dataset.downloadurl = ["text/plain", downLoadLink.download, downLoadLink.href].join(":");
downLoadLink.click();
downLoadLink.remove();
</script>
</body>
</html>
log
D test start:
D {"key":"A","data":"123"} -- From line 12 of file:///android_asset/test/test.html
D onDownloadStart: blob:file:///bf92b937-be38-43d4-9bda-ad4e1e7b98ae
D contentDisposition: mimetype:text/plain contentlength:24
E onDownloadStart:
java.io.FileNotFoundException: No content provider: blob:file:///bf92b937-be38-43d4-9bda-ad4e1e7b98ae
at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:1673)
at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:1503)
at android.content.ContentResolver.openInputStream(ContentResolver.java:1187)
at olto.java_conf.gr.jp.testapp.MainActivity$2.onDownloadStart(MainActivity.java:71)
at WV.B6.handleMessage(chromium-TrichromeWebViewGoogle.aab-stable-609914436:483)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)