广告
返回顶部
首页 > 资讯 > 移动开发 >Android webview打开本地图片上传实现代码
  • 516
分享到

Android webview打开本地图片上传实现代码

图片上传图片地图webviewAndroid 2022-06-06 04:06:15 516人浏览 泡泡鱼
摘要

WEBview打开本地图片选择器十分之麻烦,其在安卓系统3x 4x 5x上的行为都不同,处理也不同,所以之前差点崩溃。经过测试和完善,最终其在各个版本上都能完美工作。 直接

WEBview打开本地图片选择器十分之麻烦,其在安卓系统3x 4x 5x上的行为都不同,处理也不同,所以之前差点崩溃。经过测试和完善,最终其在各个版本上都能完美工作。

直接上代码


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提示").setMessage(message).setPositiveButton("确定", null);
        builder.setCancelable(false);
        builder.setIcon(R.mipmap.ic_launcher);
        AlertDialog dialog = builder.create();
        dialog.show();
        result.confirm();
        return true;
      }
      //扩展浏览器上传文件
      //3.0++版本
      public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) {
        openFileChooserImpl(uploadMsg);
      }
      //3.0--版本
      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/*");
    startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);
  }
  public ValueCallback<Uri[]> mUploadMessageForAndroid5;
  private void onenFileChooseImpleForAndroid(ValueCallback<Uri[]> filePathCallback) {
    mUploadMessageForAndroid5 = filePathCallback;
    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_5);
  }
  @Override
  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;
    } 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) {
      //获取历史列表
      WebBackForwardList mWebBackForwardList = webView
          .copyBackForwardList();
      //判断当前历史列表是否最顶端,其实canGoBack已经判断过
      if (mWebBackForwardList.getCurrentIndex() > 0) {
        webView.goBack();
        return true;
      }
    }
    return super.onKeyDown(keyCode, event);
  }
}
您可能感兴趣的文章:Android开发实现webview中img标签加载本地图片的方法Android实现点击WebView界面中图片滑动浏览与保存图片功能Android 实现WebView点击图片查看大图列表及图片保存功能Android中WebView图片实现自适应的方法Android WebView中图片浏览及缩放效果


--结束END--

本文标题: Android webview打开本地图片上传实现代码

本文链接: https://www.lsjlt.com/news/22270.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作