分类

链接

2016 年 5 月
 1
2345678
9101112131415
16171819202122
23242526272829
3031  

近期文章

热门标签

新人福利,免费薅羊毛

现在位置:    首页 > Android > 正文
共享办公室出租
解决android webview无法上传的解决办法
Android 评论(1) 阅读(2,004)

重写WebChromeClient,完成代码如下:

package com.cxwl.nst.supplyinfor.activity;

import Android.graphics.Bitmap;
import android.net.Uri;
import android.os.Message;
import android.view.View;
import android.webkit.ConsoleMessage;
import android.webkit.GeolocationPermissions;
import android.webkit.JsPromptResult;
import android.webkit.JsResult;
import android.webkit.ValueCallback;
import android.webkit.WebChromeClient;
import android.webkit.WebStorage;
import android.webkit.WebView;

abstract class TestWebChromeClient extends WebChromeClient {
private WebChromeClient mWrappedClient;

protected TestWebChromeClient(WebChromeClient wrappedClient) {
mWrappedClient = wrappedClient;
}

/** } */
@Override
public void onProgressChanged(WebView view, int newProgress) {
mWrappedClient.onProgressChanged(view, newProgress);
}

/** } */
@Override
public void onReceivedTitle(WebView view, String title) {
mWrappedClient.onReceivedTitle(view, title);
}

/** } */
@Override
public void onReceivedIcon(WebView view, Bitmap icon) {
mWrappedClient.onReceivedIcon(view, icon);
}

/** } */
@Override
public void onReceivedTouchIconUrl(WebView view, String url,
boolean precomposed) {
mWrappedClient.onReceivedTouchIconUrl(view, url, precomposed);
}

/** } */
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
mWrappedClient.onShowCustomView(view, callback);
}

/** } */
@Override
public void onHideCustomView() {
mWrappedClient.onHideCustomView();
}

/** } */
@Override
public boolean onCreateWindow(WebView view, boolean dialog,
boolean userGesture, Message resultMsg) {
return mWrappedClient.onCreateWindow(view, dialog, userGesture, resultMsg);
}

/** } */
@Override
public void onRequestFocus(WebView view) {
mWrappedClient.onRequestFocus(view);
}

/** } */
@Override
public void onCloseWindow(WebView window) {
mWrappedClient.onCloseWindow(window);
}

/** } */
@Override
public boolean onJsAlert(WebView view, String url, String message,
JsResult result) {
return mWrappedClient.onJsAlert(view, url, message, result);
}

/** } */
@Override
public boolean onJsConfirm(WebView view, String url, String message,
JsResult result) {
return mWrappedClient.onJsConfirm(view, url, message, result);
}

/** } */
@Override
public boolean onJsPrompt(WebView view, String url, String message,
String defaultValue, JsPromptResult result) {
return mWrappedClient.onJsPrompt(view, url, message, defaultValue, result);
}

/** } */
@Override
public boolean onJsBeforeUnload(WebView view, String url, String message,
JsResult result) {
return mWrappedClient.onJsBeforeUnload(view, url, message, result);
}

/** } */
@Override
public void onExceededDatabaseQuota(String url, String databaseIdentifier,
long currentQuota, long estimatedSize, long totalUsedQuota,
WebStorage.QuotaUpdater quotaUpdater) {
mWrappedClient.onExceededDatabaseQuota(url, databaseIdentifier, currentQuota,
estimatedSize, totalUsedQuota, quotaUpdater);
}

/** } */
@Override
public void onReachedMaxAppCacheSize(long spaceNeeded, long totalUsedQuota,
WebStorage.QuotaUpdater quotaUpdater) {
mWrappedClient.onReachedMaxAppCacheSize(spaceNeeded, totalUsedQuota, quotaUpdater);
}

/** } */
@Override
public void onGeolocationPermissionsShowPrompt(String origin,
GeolocationPermissions.Callback callback) {
mWrappedClient.onGeolocationPermissionsShowPrompt(origin, callback);
}

/** } */
@Override
public void onGeolocationPermissionsHidePrompt() {
mWrappedClient.onGeolocationPermissionsHidePrompt();
}

/** } */
@Override
public boolean onJsTimeout() {
return mWrappedClient.onJsTimeout();
}

/** } */
@Override
@Deprecated
public void onConsoleMessage(String message, int lineNumber, String sourceID) {
mWrappedClient.onConsoleMessage(message, lineNumber, sourceID);
}

/** } */
@Override
public boolean onConsoleMessage(ConsoleMessage consoleMessage) {
return mWrappedClient.onConsoleMessage(consoleMessage);
}

/** } */
@Override
public Bitmap getDefaultVideoPoster() {
return mWrappedClient.getDefaultVideoPoster();
}

/** } */
@Override
public View getVideoLoadingProgressView() {
return mWrappedClient.getVideoLoadingProgressView();
}

/** } */
@Override
public void getVisitedHistory(ValueCallback<String[]> callback) {
mWrappedClient.getVisitedHistory(callback);
}

/** } */

public void openFileChooser(ValueCallback<Uri> uploadFile) {
((TestWebChromeClient) mWrappedClient).openFileChooser(uploadFile);
}
}

 

 

2、activity页面引用

a、初始化 全局变量

private ValueCallback<Uri> mUploadMessage;
private final static int FILECHOOSER_RESULTCODE = 1;

b、onCreate中写如下代码

webView = (WebView) findViewById(R.id.supplyinfor_news_webview);
WebSettings webSettings = webView.getSettings();
webSettings.setAllowFileAccess(true);// 设置允许访问文件数据
webSettings.setJavaScriptEnabled(true);// 设置支持JavaScript脚本
webSettings.setBuiltInZoomControls(true);// 设置支持缩放
webView.getSettings().setBuiltInZoomControls(true);//启用触摸放大
webView.getSettings().setJavaScriptEnabled(true);

webView.setWebChromeClient(new TestWebChromeClient(new WebChromeClient()) {

// The undocumented magic method override
// Eclipse will swear at you if you try to put @Override here
public void openFileChooser(ValueCallback<Uri> uploadMsg) {
mUploadMessage = uploadMsg;
Intent i = new Intent(Intent.ACTION_GET_CONTENT);
i.addCategory(Intent.CATEGORY_OPENABLE);
i.setType("image/*");
supplyinforList.this.startActivityForResult(Intent.createChooser(i, getString(R.string.uploadStr)), supplyinforList.FILECHOOSER_RESULTCODE);
}
});

c、创建onActivityResult方法

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

<span class="lit">@Override</span>
<span class="kwd">public</span> <span class="kwd">void</span><span class="pln"> onCreate</span><span class="pun">(</span><span class="typ">Bundle</span><span class="pln"> savedInstanceState</span><span class="pun">)</span> <span class="pun">{</span>
    <span class="kwd">super</span><span class="pun">.</span><span class="pln">onCreate</span><span class="pun">(</span><span class="pln">savedInstanceState</span><span class="pun">);</span><span class="pln">
    setContentView</span><span class="pun">(</span><span class="pln">R</span><span class="pun">.</span><span class="pln">layout</span><span class="pun">.</span><span class="pln">main</span><span class="pun">);</span><span class="pln">

    web </span><span class="pun">=</span> <span class="pun">(</span><span class="typ">WebView</span><span class="pun">)</span><span class="pln"> findViewById</span><span class="pun">(</span><span class="pln">R</span><span class="pun">.</span><span class="pln">id</span><span class="pun">.</span><span class="pln">webview01</span><span class="pun">);</span><span class="pln">
    progressBar </span><span class="pun">=</span> <span class="pun">(</span><span class="typ">ProgressBar</span><span class="pun">)</span><span class="pln"> findViewById</span><span class="pun">(</span><span class="pln">R</span><span class="pun">.</span><span class="pln">id</span><span class="pun">.</span><span class="pln">progressBar1</span><span class="pun">);</span><span class="pln">

    web </span><span class="pun">=</span> <span class="kwd">new</span> <span class="typ">WebView</span><span class="pun">(</span><span class="kwd">this</span><span class="pun">);</span><span class="pln">  
    web</span><span class="pun">.</span><span class="pln">getSettings</span><span class="pun">().</span><span class="pln">setJavaScriptEnabled</span><span class="pun">(</span><span class="kwd">true</span><span class="pun">);</span><span class="pln">
    web</span><span class="pun">.</span><span class="pln">loadUrl</span><span class="pun">(</span><span class="str">"http://www.script-tutorials.com/demos/199/index.html"</span><span class="pun">);</span><span class="pln">
    web</span><span class="pun">.</span><span class="pln">setWebViewClient</span><span class="pun">(</span><span class="kwd">new</span><span class="pln"> myWebClient</span><span class="pun">());</span><span class="pln">
    web</span><span class="pun">.</span><span class="pln">setWebChromeClient</span><span class="pun">(</span><span class="kwd">new</span> <span class="typ">WebChromeClient</span><span class="pun">()</span>  
    <span class="pun">{</span>  
           <span class="com">//The undocumented magic method override  </span>
           <span class="com">//Eclipse will swear at you if you try to put @Override here  </span>
        <span class="com">// For Android 3.0+</span>
        <span class="kwd">public</span> <span class="kwd">void</span><span class="pln"> openFileChooser</span><span class="pun">(</span><span class="typ">ValueCallback</span><span class="pun">&lt;</span><span class="typ">Uri</span><span class="pun">&gt;</span><span class="pln"> uploadMsg</span><span class="pun">)</span> <span class="pun">{</span><span class="pln">  

            mUploadMessage </span><span class="pun">=</span><span class="pln"> uploadMsg</span><span class="pun">;</span>  
            <span class="typ">Intent</span><span class="pln"> i </span><span class="pun">=</span> <span class="kwd">new</span> <span class="typ">Intent</span><span class="pun">(</span><span class="typ">Intent</span><span class="pun">.</span><span class="pln">ACTION_GET_CONTENT</span><span class="pun">);</span><span class="pln">  
            i</span><span class="pun">.</span><span class="pln">addCategory</span><span class="pun">(</span><span class="typ">Intent</span><span class="pun">.</span><span class="pln">CATEGORY_OPENABLE</span><span class="pun">);</span><span class="pln">  
            i</span><span class="pun">.</span><span class="pln">setType</span><span class="pun">(</span><span class="str">"image/*"</span><span class="pun">);</span>  
            <span class="typ">MyWb</span><span class="pun">.</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">startActivityForResult</span><span class="pun">(</span><span class="typ">Intent</span><span class="pun">.</span><span class="pln">createChooser</span><span class="pun">(</span><span class="pln">i</span><span class="pun">,</span><span class="str">"File Chooser"</span><span class="pun">),</span><span class="pln"> FILECHOOSER_RESULTCODE</span><span class="pun">);</span>  

           <span class="pun">}</span>

        <span class="com">// For Android 3.0+</span>
           <span class="kwd">public</span> <span class="kwd">void</span><span class="pln"> openFileChooser</span><span class="pun">(</span> <span class="typ">ValueCallback</span><span class="pln"> uploadMsg</span><span class="pun">,</span> <span class="typ">String</span><span class="pln"> acceptType </span><span class="pun">)</span> <span class="pun">{</span><span class="pln">
           mUploadMessage </span><span class="pun">=</span><span class="pln"> uploadMsg</span><span class="pun">;</span>
           <span class="typ">Intent</span><span class="pln"> i </span><span class="pun">=</span> <span class="kwd">new</span> <span class="typ">Intent</span><span class="pun">(</span><span class="typ">Intent</span><span class="pun">.</span><span class="pln">ACTION_GET_CONTENT</span><span class="pun">);</span><span class="pln">
           i</span><span class="pun">.</span><span class="pln">addCategory</span><span class="pun">(</span><span class="typ">Intent</span><span class="pun">.</span><span class="pln">CATEGORY_OPENABLE</span><span class="pun">);</span><span class="pln">
           i</span><span class="pun">.</span><span class="pln">setType</span><span class="pun">(</span><span class="str">"*/*"</span><span class="pun">);</span>
           <span class="typ">MyWb</span><span class="pun">.</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">startActivityForResult</span><span class="pun">(</span>
           <span class="typ">Intent</span><span class="pun">.</span><span class="pln">createChooser</span><span class="pun">(</span><span class="pln">i</span><span class="pun">,</span> <span class="str">"File Browser"</span><span class="pun">),</span><span class="pln">
           FILECHOOSER_RESULTCODE</span><span class="pun">);</span>
           <span class="pun">}</span>

        <span class="com">//For Android 4.1</span>
           <span class="kwd">public</span> <span class="kwd">void</span><span class="pln"> openFileChooser</span><span class="pun">(</span><span class="typ">ValueCallback</span><span class="pun">&lt;</span><span class="typ">Uri</span><span class="pun">&gt;</span><span class="pln"> uploadMsg</span><span class="pun">,</span> <span class="typ">String</span><span class="pln"> acceptType</span><span class="pun">,</span> <span class="typ">String</span><span class="pln"> capture</span><span class="pun">){</span><span class="pln">
               mUploadMessage </span><span class="pun">=</span><span class="pln"> uploadMsg</span><span class="pun">;</span>  
               <span class="typ">Intent</span><span class="pln"> i </span><span class="pun">=</span> <span class="kwd">new</span> <span class="typ">Intent</span><span class="pun">(</span><span class="typ">Intent</span><span class="pun">.</span><span class="pln">ACTION_GET_CONTENT</span><span class="pun">);</span><span class="pln">  
               i</span><span class="pun">.</span><span class="pln">addCategory</span><span class="pun">(</span><span class="typ">Intent</span><span class="pun">.</span><span class="pln">CATEGORY_OPENABLE</span><span class="pun">);</span><span class="pln">  
               i</span><span class="pun">.</span><span class="pln">setType</span><span class="pun">(</span><span class="str">"image/*"</span><span class="pun">);</span>  
               <span class="typ">MyWb</span><span class="pun">.</span><span class="kwd">this</span><span class="pun">.</span><span class="pln">startActivityForResult</span><span class="pun">(</span> <span class="typ">Intent</span><span class="pun">.</span><span class="pln">createChooser</span><span class="pun">(</span><span class="pln"> i</span><span class="pun">,</span> <span class="str">"File Chooser"</span> <span class="pun">),</span> <span class="typ">MyWb</span><span class="pun">.</span><span class="pln">FILECHOOSER_RESULTCODE </span><span class="pun">);</span>

           <span class="pun">}</span>

    <span class="pun">});</span><span class="pln">  


    setContentView</span><span class="pun">(</span><span class="pln">web</span><span class="pun">);</span>  


<span class="pun">}</span>

复制a,b,c三部分代码 基本上就可以实现upload上传

 

 

============ 欢迎各位老板打赏~ ===========

本文版权归Bruce's Blog所有,转载引用请完整注明以下信息:
本文作者:Bruce
本文地址:解决android webview无法上传的解决办法 | Bruce's Blog

发表评论

留言无头像?