English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Implementation code for uploading local images in Android webview

It is very麻烦 to open the local image selector in Webview, especially on the Android system3x 4x 5The behaviors on x are all different, and the handling is also different, so it almost crashed before. After testing and improvement, it works perfectly on all versions in the end.

Directly to the code.

package com.testandroid.webview;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.JsResult;
import android.webkit.ValueCallback;
import android.webkit.WebBackForwardList;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import com.testandroid.R;
public class WebViewActivity extends AppCompatActivity { 
  private final String TAG = WebViewActivity.class.getSimpleName();
  private Button button;
  private WebView webView;
  private String recgPic = "http://m.shitu.chinaso.com/mx/index.html";
  public final static int FILECHOOSER_RESULTCODE = 1;
  public final static int FILECHOOSER_RESULTCODE_FOR_ANDROID_5 = 2;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_web_view);
    button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
      }
    });
    initTestWebView();
  }
  private void initTestWebView() {
    webView = (WebView) findViewById(R.id.tempWebView);
    WiewSettings settings = webView.getSettings();
    settings.setJavaScriptEnabled(true);
    webView.setWebChromeClient(new WebChromeClient() {
      @Override
      public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
        AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
        builder.setTitle("xxx prompt").setMessage(message).setPositiveButton("OK", null);
        builder.setCancelable(false);
        builder.setIcon(R.mipmap.ic_launcher);
        AlertDialog dialog = builder.create();
        dialog.show();
        result.confirm();
        return true;
      }
      //Extend browser to upload files
      //3.0++version
      public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
        openFileChooserImpl(uploadMsg);
      }
      //3.0--version
      public void openFileChooser(ValueCallback<Uri> uploadMsg) {
        openFileChooserImpl(uploadMsg);
      }
      public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) {
        openFileChooserImpl(uploadMsg);
      }
      @Override
      public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
        onenFileChooseImpleForAndroid(filePathCallback);
        return true;
      }
    });
    webView.setWebViewClient(new WebViewClient() {
      @Override
      public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
      }
    });
    webView.loadUrl(recgPic);
  }
  public ValueCallback<Uri> mUploadMessage;
  private void openFileChooserImpl(ValueCallback<Uri> uploadMsg) {
    mUploadMessage = uploadMsg;
    Intent i = new Intent(Intent.ACTION_GET_CONTENT);
    i.addCategory(Intent.CATEGORY_OPENABLE);
    i.setType("image"/*Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
    startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
  }
  public ValueCallback<Uri[]> mUploadMessageForAndroid5;
  private void onenFileChooseImpleForAndroid(ValueCallback<Uri[]> filePathCallback) {
    mUploadMessageForAndroid5 Intent contentSelectionIntent = new Intent(Intent.ACTION_GET_CONTENT);
    contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);
    contentSelectionIntent.setType("image
    ");/*Intent chooserIntent = new Intent(Intent.ACTION_CHOOSER);
    chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);
    chooserIntent.putExtra(Intent.EXTRA_TITLE, "Image Chooser");
    startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE_FOR_ANDROID_
    );5protected void onActivityResult(int requestCode, int resultCode,Intent intent) {
  }
  @Override
  if (requestCode == FILECHOOSER_RESULTCODE) {
    if (null == mUploadMessage)
      Uri result = intent == null || resultCode != RESULT_OK &#
        return;
      mUploadMessage.onReceiveValue(result);63; null: intent.getData();
      mUploadMessage = null;
      else if (requestCode == FILECHOOSER_RESULTCODE_FOR_ANDROID_
    }5{
      if (null == mUploadMessageForAndroid5)
        return;
      Uri result = (intent == null || resultCode != RESULT_OK) ? null: intent.getData();
      if (result != null) {
        mUploadMessageForAndroid5.onReceiveValue(new Uri[]{result});
      } else {
        mUploadMessageForAndroid5.onReceiveValue(new Uri[]{});
      }
      mUploadMessageForAndroid5 = null;
    }
  }
  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (webView.canGoBack() && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
      //Get the history list
      WebBackForwardList mWebBackForwardList = webView
          .copyBackForwardList();
      //Determine whether the current history list is at the top, in fact, canGoBack has already judged
      if (mWebBackForwardList.getCurrentIndex() > 0) {
        webView.goBack();
        return true;
      }
    }
    return super.onKeyDown(keyCode, event);
  }
}

That's all for this article, I hope it will be helpful to everyone's learning, and I also hope everyone will support the Yelling Tutorial more.

Statement: The content of this article is from the Internet, the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, does not edit the content manually, and does not bear relevant legal liabilities. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report violations, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)

You May Also Like