一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

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

时间:2017-04-16 编辑:简简单单 来源:一聚教程网

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

直接上代码

 

 代码如下 复制代码

packagecom.testandroid.webview;

 

importandroid.content.Intent;

importandroid.net.Uri;

importandroid.os.Bundle;

importandroid.support.v7.app.AlertDialog;

importandroid.support.v7.app.AppCompatActivity;

importandroid.view.KeyEvent;

importandroid.view.View;

importandroid.webkit.JsResult;

importandroid.webkit.ValueCallback;

importandroid.webkit.WebBackForwardList;

importandroid.webkit.WebChromeClient;

importandroid.webkit.WebView;

importandroid.webkit.WebViewClient;

importandroid.widget.Button;

 

importcom.testandroid.R;

 

publicclassWebViewActivityextendsAppCompatActivity {

 

  privatefinalString TAG = WebViewActivity.class.getSimpleName();

 

  privateButton button;

  privateWebView webView;

 

  privateString recgPic ="http://m.shitu.chinaso.com/mx/index.html";

 

  publicfinalstaticintFILECHOOSER_RESULTCODE =1;

  publicfinalstaticintFILECHOOSER_RESULTCODE_FOR_ANDROID_5 =2;

  @Override

  protectedvoidonCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_web_view);

 

    button = (Button) findViewById(R.id.button);

    button.setOnClickListener(newView.OnClickListener() {

      @Override

      publicvoidonClick(View v) {

      }

    });

 

    initTestWebView();

 

  }

 

  privatevoidinitTestWebView() {

    webView = (WebView) findViewById(R.id.tempWebView);

 

    WiewSettings settings = webView.getSettings();

    settings.setJavaScriptEnabled(true);

 

    webView.setWebChromeClient(newWebChromeClient() {

      @Override

      publicbooleanonJsAlert(WebView view, String url, String message, JsResult result) {

        AlertDialog.Builder builder =newAlertDialog.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();

        returntrue;

      }

 

      //扩展浏览器上传文件

      //3.0++版本

      publicvoidopenFileChooser(ValueCallback uploadMsg, String acceptType) {

        openFileChooserImpl(uploadMsg);

      }

 

      //3.0--版本

      publicvoidopenFileChooser(ValueCallback uploadMsg) {

        openFileChooserImpl(uploadMsg);

      }

 

      publicvoidopenFileChooser(ValueCallback uploadMsg, String acceptType, String capture) {

        openFileChooserImpl(uploadMsg);

      }

 

      @Override

      publicbooleanonShowFileChooser(WebView  webView, ValueCallback filePathCallback, FileChooserParams  fileChooserParams) {

        onenFileChooseImpleForAndroid(filePathCallback);

        returntrue;

      }

    });

 

    webView.setWebViewClient(newWebViewClient() {

      @Override

      publicbooleanshouldOverrideUrlLoading(WebView view, String url) {

        view.loadUrl(url);

        returntrue;

      }

    });

    webView.loadUrl(recgPic);

 

 

  }

 

  publicValueCallback mUploadMessage;

  privatevoidopenFileChooserImpl(ValueCallback uploadMsg) {

    mUploadMessage = uploadMsg;

    Intent i =newIntent(Intent.ACTION_GET_CONTENT);

    i.addCategory(Intent.CATEGORY_OPENABLE);

    i.setType("image/*");

    startActivityForResult(Intent.createChooser(i,"File Chooser"), FILECHOOSER_RESULTCODE);

  }

 

  publicValueCallback mUploadMessageForAndroid5;

  privatevoidonenFileChooseImpleForAndroid(ValueCallback filePathCallback) {

    mUploadMessageForAndroid5 = filePathCallback;

    Intent contentSelectionIntent =newIntent(Intent.ACTION_GET_CONTENT);

    contentSelectionIntent.addCategory(Intent.CATEGORY_OPENABLE);

    contentSelectionIntent.setType("image/*");

 

    Intent chooserIntent =newIntent(Intent.ACTION_CHOOSER);

    chooserIntent.putExtra(Intent.EXTRA_INTENT, contentSelectionIntent);

    chooserIntent.putExtra(Intent.EXTRA_TITLE,"Image Chooser");

 

    startActivityForResult(chooserIntent, FILECHOOSER_RESULTCODE_FOR_ANDROID_5);

  }

 

  @Override

  protectedvoidonActivityResult(intrequestCode,intresultCode,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;

 

    }elseif(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(newUri[]{result});

      }else{

        mUploadMessageForAndroid5.onReceiveValue(newUri[]{});

      }

      mUploadMessageForAndroid5 =null;

    }

  }

 

  @Override

  publicbooleanonKeyDown(intkeyCode, KeyEvent event) {

    if(webView.canGoBack() && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {

      //获取历史列表

      WebBackForwardList mWebBackForwardList = webView

          .copyBackForwardList();

      //判断当前历史列表是否最顶端,其实canGoBack已经判断过

      if(mWebBackForwardList.getCurrentIndex() >0) {

        webView.goBack();

        returntrue;

      }

    }

    returnsuper.onKeyDown(keyCode, event);

  }

}

 

热门栏目