public class MainActivity extends AppCompatActivity {
private ValueCallback<Uri[]> fileChooserCallback;
private WebView mbrowser;
Uri home = Uri.parse("https://цукмсцумкцу.ru/choser.php");
//загрузки изображений
private ValueCallback<Uri> mUploadMessage;
public ValueCallback<Uri[]> uploadMessage;
public static final int REQUEST_SELECT_FILE = 100;
public static final int REQUEST_QR_CODE = 200;
private final static int FILECHOOSER_RESULTCODE = 1;
public final static int CHILD_FINISH = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mbrowser=(WebView) findViewById(R.id.main_webview);
mbrowser.getSettings().setJavaScriptEnabled(true); // разрешен javascript
mbrowser.getSettings().setDatabaseEnabled(true); // хранение данных во встроенной БД в браузере
mbrowser.getSettings().setDomStorageEnabled(true);
mbrowser.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); // разрешать открывать окна
mbrowser.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView vw, WebResourceRequest request) {
if (request.getUrl().toString().contains(home.getHost())) {
vw.loadUrl(request.getUrl().toString());
} else {
Intent intent = new Intent(Intent.ACTION_VIEW, request.getUrl());
vw.getContext().startActivity(intent);
}
return true;
}
});
mbrowser.setWebChromeClient(new WebChromeClient() {
@Override
public void onPermissionRequest(final PermissionRequest request) {
request.grant(request.getResources());
}
@Override
public boolean onShowFileChooser(WebView vw, ValueCallback<Uri[]> filePathCallback,
FileChooserParams fileChooserParams) {
if (fileChooserCallback != null) {
fileChooserCallback.onReceiveValue(null);
}
fileChooserCallback = filePathCallback;
Intent selectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
selectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
selectionIntent.setType("*/*");
Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
chooserIntent.putExtra(Intent.EXTRA_INTENT, selectionIntent);
startActivityForResult(chooserIntent, 0);
return true;
}
});
mbrowser.setOnKeyListener((v, keyCode, event) ->
{
WebView vw = (WebView) v;
if (event.getAction() == KeyEvent.ACTION_DOWN && keyCode == KeyEvent.KEYCODE_BACK && vw.canGoBack()) {
vw.goBack();
return true;
}
return false;
});
mbrowser.setDownloadListener((uri, userAgent, contentDisposition, mimetype, contentLength) -> handleURI(uri));
mbrowser.setOnLongClickListener(v -> {
handleURI(((WebView) v).getHitTestResult().getExtra());
return true;
});
mbrowser.loadUrl(home.toString());
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
fileChooserCallback.onReceiveValue(new Uri[]{Uri.parse(intent.getDataString())});
fileChooserCallback = null;
}
private void handleURI(String uri) {
if (uri != null) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(uri.replaceFirst("^blob:", "")));
startActivity(i);
}
}
}