Hi
I have an app that uses webview and uses loadurl with file:\\\ but that doesn't work anymore and I can't figure out how to get it to work with content provider because I use local asset html files, any one who can help me ? It works first time when the app loads, but when I do a location.reload() in javascript it can't find the html file when it tries to reload it it throws an exception android.os.FileUriExposedException. I have red that whne you are using api > 24 you can't use file:/// but should use a content provider but I can't figure out how to do that
Here is my mainactivity:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
final Context app = this;
String language = Locale.getDefault().getLanguage().toLowerCase();
WebView webView;
private class WebViewClient extends WebChromeClient {
private WebViewClient(MainActivity mainActivity, Object o) {
}
public boolean onJsConfirm(WebView webview, String s, String message, final JsResult jsResult) {
new Builder(MainActivity.this.app).setTitle(R.string.warning).setMessage(message).setPositiveButton(R.string.alert_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
jsResult.confirm();
}
}).setNegativeButton(R.string.alert_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
jsResult.cancel();
}
}).create().show();
return true;
}
}
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
this.webView = findViewById(R.id.Main);
this.webView.getSettings().setJavaScriptEnabled(true);
this.webView.getSettings().setAllowContentAccess(true);
this.webView.getSettings().setAllowFileAccess(true);
this.webView.getSettings().setAppCacheEnabled(true);
this.webView.getSettings().setDatabaseEnabled(true);
this.webView.getSettings().setDomStorageEnabled(true);
this.webView.getSettings().setAllowFileAccessFromFileURLs(true);
//StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
//StrictMode.setVmPolicy(builder.build());
if(this.language.equals("da")) {
this.webView.loadUrl("file:///android_asset/www/da/index.html");
} else if (this.language.equals("de")) {
this.webView.loadUrl("file:///android_asset/www/de/index.html");
} else {
this.webView.loadUrl("file:///android_asset/www/en/index.html");
}
this.webView.setWebChromeClient(new WebViewClient(this, null));
this.webView.setDownloadListener(new DownloadListener() {
@override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
Intent intent = new Intent("android.intent.action.VIEW");
intent.setDataAndType(Uri.parse(url), mimetype);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
MainActivity.this.startActivity(intent);
}
});
}
I have an app that uses webview and uses loadurl with file:\\\ but that doesn't work anymore and I can't figure out how to get it to work with content provider because I use local asset html files, any one who can help me ? It works first time when the app loads, but when I do a location.reload() in javascript it can't find the html file when it tries to reload it it throws an exception android.os.FileUriExposedException. I have red that whne you are using api > 24 you can't use file:/// but should use a content provider but I can't figure out how to do that
Here is my mainactivity:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
final Context app = this;
String language = Locale.getDefault().getLanguage().toLowerCase();
WebView webView;
private class WebViewClient extends WebChromeClient {
private WebViewClient(MainActivity mainActivity, Object o) {
}
public boolean onJsConfirm(WebView webview, String s, String message, final JsResult jsResult) {
new Builder(MainActivity.this.app).setTitle(R.string.warning).setMessage(message).setPositiveButton(R.string.alert_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
jsResult.confirm();
}
}).setNegativeButton(R.string.alert_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
jsResult.cancel();
}
}).create().show();
return true;
}
}
@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
this.webView = findViewById(R.id.Main);
this.webView.getSettings().setJavaScriptEnabled(true);
this.webView.getSettings().setAllowContentAccess(true);
this.webView.getSettings().setAllowFileAccess(true);
this.webView.getSettings().setAppCacheEnabled(true);
this.webView.getSettings().setDatabaseEnabled(true);
this.webView.getSettings().setDomStorageEnabled(true);
this.webView.getSettings().setAllowFileAccessFromFileURLs(true);
//StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
//StrictMode.setVmPolicy(builder.build());
if(this.language.equals("da")) {
this.webView.loadUrl("file:///android_asset/www/da/index.html");
} else if (this.language.equals("de")) {
this.webView.loadUrl("file:///android_asset/www/de/index.html");
} else {
this.webView.loadUrl("file:///android_asset/www/en/index.html");
}
this.webView.setWebChromeClient(new WebViewClient(this, null));
this.webView.setDownloadListener(new DownloadListener() {
@override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
Intent intent = new Intent("android.intent.action.VIEW");
intent.setDataAndType(Uri.parse(url), mimetype);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
MainActivity.this.startActivity(intent);
}
});
}
Last edited: