• webview
  • 代码示例

webview

在android的组件webview中是不能用H5上传文件了的。但是有时候我们需要用H5去调用上传文件,因此需要支持这项功能。

代码示例

有不同的额版本适配方法 3.0 4.0 5.0 6.0

public class PlayActivity extends AppCompatActivity {    @Override    protected void onDestroy() {        super.onDestroy();    }    /**     * Called when the activity is first created.     */    WebView web;    private ValueCallback mUploadMessage;    public ValueCallback uploadMessage;    public static final int REQUEST_SELECT_FILE = 100;    private final static int FILECHOOSER_RESULTCODE = 1;    @Override    protected void onActivityResult(int requestCode, int resultCode,                                    Intent intent) {        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {            if (requestCode == REQUEST_SELECT_FILE) {                if (uploadMessage == null)                    return;                uploadMessage.onReceiveValue(WebChromeClient.FileChooserParams.parseResult(resultCode, intent));                uploadMessage = null;            }        } else if (requestCode == FILECHOOSER_RESULTCODE) {            if (null == mUploadMessage)                return;            // Use MainActivity.RESULT_OK if you're implementing WebView inside Fragment            // Use RESULT_OK only if you're implementing WebView inside an Activity            Uri result = intent == null || resultCode != MainActivity.RESULT_OK ? null : intent.getData();            mUploadMessage.onReceiveValue(result);            mUploadMessage = null;        }    }    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_play);        web = (WebView) findViewById(R.id.webview01);        web = new WebView(this);        web.getSettings().setJavaScriptEnabled(true);        web.loadUrl("http://10.0.0.82:1346/hot/7516063/A1035B1ECD35AA7F0FAE050007F01000209");        web.setWebViewClient(new myWebClient());        web.setWebChromeClient(new WebChromeClient() {            //The undocumented magic method override            //Eclipse will swear at you if you try to put @Override here            // For Android 3.0+            public void openFileChooser(ValueCallback uploadMsg) {                mUploadMessage = uploadMsg;                Intent i = new Intent(Intent.ACTION_GET_CONTENT);                i.addCategory(Intent.CATEGORY_OPENABLE);                i.setType("image/*");                PlayActivity.this.startActivityForResult(Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE);            }            public void openFileChooser(ValueCallback uploadMsg, String acceptType) {                openFileChooser(uploadMsg);            }            public void openFileChooser(ValueCallback uploadMsg, String acceptType, String capture) {                openFileChooser(uploadMsg);            }            // For Lollipop 5.0+ Devices            @TargetApi(Build.VERSION_CODES.LOLLIPOP)            public boolean onShowFileChooser(WebView mWebView, ValueCallback filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {                if (uploadMessage != null) {                    uploadMessage.onReceiveValue(null);                    uploadMessage = null;                }                uploadMessage = filePathCallback;                Intent intent = fileChooserParams.createIntent();                try {                    startActivityForResult(intent, REQUEST_SELECT_FILE);                } catch (ActivityNotFoundException e) {                    uploadMessage = null;                    return false;                }                return true;            }        });        setContentView(web);    }    public class myWebClient extends WebViewClient {        @Override        public void onPageStarted(WebView view, String url, Bitmap favicon) {            // TODO Auto-generated method stub            super.onPageStarted(view, url, favicon);        }        @Override        public boolean shouldOverrideUrlLoading(WebView view, String url) {            // TODO Auto-generated method stub            view.loadUrl(url);            return true;        }        @Override        public void onPageFinished(WebView view, String url) {            // TODO Auto-generated method stub            super.onPageFinished(view, url);        }    }    //flipscreen not loading again    @Override    public void onConfigurationChanged(Configuration newConfig) {        super.onConfigurationChanged(newConfig);    }}

更多相关文章

  1. android短信服务
  2. Android(安卓)中各种XML文件的作用
  3. Android第五十二期 - 云之讯的代码混淆
  4. Android中的strings文件中字符串的拼接
  5. android基础-界面代码
  6. Android(安卓)使用自定义注解代替重复写findViewById代码
  7. Android(安卓)startActivityForResult的使用
  8. Android代码风格指导
  9. Android学习心得(六)——位置服务

随机推荐

  1. Android通过代码打开和关闭网络连接
  2. 移动App开发(包含iOS和Android转型)
  3. android UI设计之思考
  4. Android视频背景,动态背景,Android用视频
  5. Android(安卓)UI设计系列之自定义TextVie
  6. 如何使用Android(安卓)UI Fragment开发“
  7. Android之数据持久化(sqllite数据库)四
  8. Android实现图片相似度
  9. Android中为ViewGroup设置selector无效果
  10. Android之5大优点5大劣势