English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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); } }
이것이 본 문서의 전체 내용입니다. 많은 도움이 되길 바라며, 많은 지원을 해 주시길 바랍니다.
명시: 본문의 내용은 인터넷에서 가져왔으며, 저작권자는 본인입니다. 인터넷 사용자가 자발적으로 기여하고 자신의 사이트에 업로드한 내용으로, 이 사이트는 소유권을 가지지 않으며, 인공적으로 편집된 내용이 없으며, 관련 법적 책임도 부담하지 않습니다. 저작권 침해가 의심되는 내용이 있다면, notice#w로 이메일을 보내 주세요.3codebox.com(보고서를 작성할 때는 #을 @으로 변경하십시오. 신고하고 관련 증거를 제공하시면, 실제로 확인되면 해당 사이트는 즉시 저작권 침해 내용을 삭제합니다。)