91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android webview打開本地圖片上傳實現代碼

發布時間:2020-10-14 08:33:06 來源:腳本之家 閱讀:108 作者:ShouCeng 欄目:移動開發

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);
  }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持億速云。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

罗城| 辉县市| 淮阳县| 中牟县| 浦江县| 元江| 吴江市| 桐梓县| 临城县| 周口市| 商城县| 油尖旺区| 博乐市| 饶阳县| 抚远县| 宁都县| 桐乡市| 保靖县| 义马市| 余庆县| 许昌县| 梁山县| 肃南| 兰西县| 南皮县| 安平县| 轮台县| 岑巩县| 和林格尔县| 东乡县| 马龙县| 湖南省| 江孜县| 象山县| 长泰县| 长宁县| 临江市| 汉寿县| 和平县| 板桥市| 玛纳斯县|