В webView не работает кнопка которая расположена на сайте, кнопка отвечает за загрузку картинки.
Добавлено: 12 ноя 2019, 01:20
В webView не работает кнопка которая расположена на сайте, кнопка отвечает за загрузку картинки. Примеров нашёл много но есть одно но мой webView расположен на фрагменте.
Вот мой код, может ли кто нибудь натолкнуть или помочь написать правильный код.
main.xml
Main.java
Вот мой код, может ли кто нибудь натолкнуть или помочь написать правильный код.
main.xml
Код: Выделить всё
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Fragments.Main">
<View
android:id="@+id/d1"
android:layout_marginTop="5dp"
android:layout_height="5dp"
android:layout_marginBottom="5dp"
android:layout_width="fill_parent"
android:background="@drawable/css_orangefon"/>
<WebView
android:id="@+id/webView"
android:layout_below="@+id/d1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="RedundantNamespace" />
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_centerInParent="true"
android:indeterminateDrawable="@drawable/scaledrawable"
android:visibility="visible" />
</RelativeLayout>Код: Выделить всё
package com.****.*******.*****.Fragments;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import com.google.android.gms.common.internal.service.Common;
import com.****.*******.*****.R;
import static android.app.Activity.RESULT_OK;
public class Main extends Fragment {
private ProgressBar progressBar;
@SuppressLint("SetJavaScriptEnabled")
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main, container, false);
progressBar = rootView.findViewById(R.id.progressBar);
final WebView mWebView = rootView.findViewById(R.id.webView);
mWebView.setWebViewClient(new WebViewClient());
//Для корректной работы браузера
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setDefaultTextEncodingName("utf-8");
mWebView.getSettings().setSupportZoom(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(false);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.getSettings().setDisplayZoomControls(false);
mWebView.getSettings().setAllowFileAccess(true);
//Нажатие на кнопку back (home) возвращения назад в фрагменте
mWebView.canGoBack();
mWebView.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK
&& event.getAction() == MotionEvent.ACTION_UP
&& mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return false;
}
});
//Нажатие на кнопку back (home) возвращения назад в фрагменте
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(progress);
if (progress == 100) {
progressBar.setVisibility(View.GONE); // Make the bar disappear after URL is loaded
}
}
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
}
// For Android 3.0+
public void openFileChooser(ValueCallback uploadMsg, String acceptType) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("*/*");
startActivityForResult(
Intent.createChooser(i, "File Browser"),
FILECHOOSER_RESULTCODE);
}
//For Android 4.1
public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
}
});
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
if (requestCode == FILECHOOSER_RESULTCODE) {
if (null == mUploadMessage) return;
Uri result = intent == null || resultCode != RESULT_OK ? null
: intent.getData();
mUploadMessage.onReceiveValue(result);
mUploadMessage = null;
}
}
progressBar.setVisibility(View.VISIBLE);
mWebView.loadUrl("https://мой сайт");
return rootView;
}
}