webview 选择文件
来源:互联网 发布:bp神经网络数据归一化 编辑:程序博客网 时间:2024/06/18 14:11
网页上的button定义 <input tyoe="file" />
方法一,也是网上能搜到最多的解决方案
参考网址:http://stackoverflow.com/questions/4944102/android-webview-file-input-field-filechooser-doesnt-show-up
http://m0s-programming.blogspot.tw/2011/02/file-upload-in-through-webview-on.html
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode==FILECHOOSER_RESULTCODE) { if (null == mUploadMessage) return; Uri result = data == null || resultCode != RESULT_OK ? null : data.getData(); mUploadMessage.onReceiveValue(result); mUploadMessage = null; } }
在WebChromeClient的设定中加入如下代码:
setting.setWebChromeClient(new WebChromeClient() { // For Android 3.0+ public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) { if (mUploadMessage != null) return; mUploadMessage = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("*/*"); startActivityForResult( Intent.createChooser( i, "File Chooser" ), BrowserActivity.FILECHOOSER_RESULTCODE ); } // For Android < 3.0 public void openFileChooser(ValueCallback<Uri> uploadMsg) { openFileChooser( uploadMsg, "" ); } }); // For Android > 4.1.1 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { openFileChooser(uploadMsg, acceptType); }
openFileChooser为隐藏方法。
这样可以实现打开目录上传文件。不过在3.0以后的版本中,如果同样的网页在android自带的浏览器打开会发现,
还有照相,录音的功能,然后直接将生成的图片或音频文件上传,这样就要使用下面的方法。
方法二,参考自com.android.browser的源码程式。
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode==FILECHOOSER_RESULTCODE) { if (null == mUploadMessage) return; Uri result = data == null || resultCode != RESULT_OK ? null : data.getData(); if (result == null && data == null && resultCode == Activity.RESULT_OK) { File cameraFile = new File(mCameraFilePath); if (cameraFile.exists()) { result = Uri.fromFile(cameraFile); // Broadcast to the media scanner that we have a new photo // so it will be added into the gallery for the user. sendBroadcast( new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, result)); } } mUploadMessage.onReceiveValue(result); mUploadMessage = null; } }
在WebChromeClient的设定中加入如下代码:
setting.setWebChromeClient(new WebChromeClient() { // For Android 3.0+ public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType) { if (mUploadMessage != null) return; mUploadMessage = uploadMsg; startActivityForResult(createDefaultOpenableIntent(), BrowserActivity.FILECHOOSER_RESULTCODE); } } // For Android < 3.0 public void openFileChooser(ValueCallback<Uri> uploadMsg) { openFileChooser( uploadMsg, "" ); } });
private Intent createDefaultOpenableIntent() { // Create and return a chooser with the default OPENABLE // actions including the camera, camcorder and sound // recorder where available. Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("*/*"); Intent chooser = createChooserIntent(createCameraIntent(), createCamcorderIntent(), createSoundRecorderIntent()); chooser.putExtra(Intent.EXTRA_INTENT, i); return chooser; } private Intent createChooserIntent(Intent... intents) { Intent chooser = new Intent(Intent.ACTION_CHOOSER); chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, intents); chooser.putExtra(Intent.EXTRA_TITLE, "File Chooser"); return chooser; } private Intent createCameraIntent() { Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); File externalDataDir = Environment.getExternalStoragePublicDirectory( Environment.DIRECTORY_DCIM); File cameraDataDir = new File(externalDataDir.getAbsolutePath() + File.separator + "browser-photos"); cameraDataDir.mkdirs(); String mCameraFilePath = cameraDataDir.getAbsolutePath() + File.separator + System.currentTimeMillis() + ".jpg"; cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(mCameraFilePath))); return cameraIntent; } private Intent createCamcorderIntent() { return new Intent(MediaStore.ACTION_VIDEO_CAPTURE); } private Intent createSoundRecorderIntent() { return new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION); }
0 0
- webview 选择文件
- Android WebView选择文件
- 【项目中---WebView】Webview打开本地图片文件选择解决方案
- Webview打开本地图片文件选择解决方案
- Android WebView 网页实现选择文件
- 网页通过webview调用Android的图片或文件选择
- Android WebView上传文件的问题 AlertDialog取消选择
- 网页通过webview调用Android的图片或文件选择
- 在Android中通过WebView调用相机拍照/选择文件
- 解决android中使用webview选择文件上传问题
- Android 内嵌WebView之选择文件上传及扩展
- 解决WebView文件上传无法重复选择问题
- webview上传图片一直提示"未选择任何文件"
- Android WebView 选择图片并上传(调用相机拍照/相册/选择文件)
- webView选择本地图片
- Android开发心得——网页通过webview调用Android的图片或文件选择
- 在Android浏览器中通过WebView调用相机拍照/选择文件 上传到服务器
- Android Webview 加载外部html时选择加载本地的js,css等资源文件
- RabbitMQ routing模式
- 如何初步掌控一台linux
- [LeetCode]Word Break
- 【移动安全】315钓鱼Wifi获取个人信息的深入剖析
- eclipse loading descriptor .....
- webview 选择文件
- 什么是"hibernate",什么是DAO设计模式???
- C++ STL Vector容器的用法
- 实现单例
- JAVA反射机制
- unity自带脚本ThirdPersonController.cs(收藏)
- python的内存管理机制
- 正则表达式语法
- 设计模式-工厂方法模式(factoryMethod pattern)