您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇文章为大家展示了Android项目中如何在webview页面中上传文件,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
Android webview在默认情况下是不支持网页中的文件上传功能的;
如果在网页中有<input type="file" />,在android webview中访问时也会出现浏览文件的按钮
但是点击按钮之后没有反应...
那么如何能够让android的webview能够响应,这个浏览按钮呢?在网上查了很多资料,很多相同的,但都漏掉了一个地方,导致无法读取到文件的完整地址(“c:\upfile\233232.jpg”),整理最终代码入下:
我们需要为webview设置WebChromeClient,在WebChromeClient的实现类中覆盖文件选择的方法:
package com.example.webviewupfile; import java.io.File; import java.io.IOException; import android.app.Activity; import android.content.ContentResolver; import android.content.Intent; import android.content.res.Configuration; import android.database.Cursor; import android.graphics.Bitmap; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.util.Log; import android.view.View; import android.webkit.ValueCallback; import android.webkit.WebChromeClient; import android.webkit.WebView; import android.webkit.WebViewClient; import android.widget.ProgressBar; public class MainActivity extends Activity { private ValueCallback<Uri> mUploadMessage; private final static int FILECHOOSER_RESULTCODE = 1; private WebView web; private ProgressBar progressBar; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); web = (WebView) findViewById(R.id.webView1); progressBar = (ProgressBar) findViewById(R.id.progressBar1); web = new WebView(this); web.getSettings().setJavaScriptEnabled(true); web.loadUrl("http://ueditor.baidu.com/website/onlinedemo.html"); 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<Uri> uploadMsg) { mUploadMessage = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("image/*"); MainActivity.this.startActivityForResult( Intent.createChooser(i, "File Chooser"), FILECHOOSER_RESULTCODE); } // For Android 3.0+ public void openFileChooser(ValueCallback uploadMsg, String acceptType) { mUploadMessage = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("*/*"); MainActivity.this.startActivityForResult( Intent.createChooser(i, "File Browser"), FILECHOOSER_RESULTCODE); } // For Android 4.1 public void openFileChooser(ValueCallback<Uri> uploadMsg, String acceptType, String capture) { mUploadMessage = uploadMsg; Intent i = new Intent(Intent.ACTION_GET_CONTENT); i.addCategory(Intent.CATEGORY_OPENABLE); i.setType("image/*"); MainActivity.this.startActivityForResult( Intent.createChooser(i, "File Chooser"), MainActivity.FILECHOOSER_RESULTCODE); } }); setContentView(web); } @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; Bitmap bm = null; //外界的程序访问ContentProvider所提供数据 可以通过ContentResolver接口 ContentResolver resolver = getContentResolver(); try { Uri originalUri = intent.getData(); // 获得图片的uri bm = MediaStore.Images.Media.getBitmap(resolver, originalUri); // 这里开始的第二部分,获取图片的路径: String[] proj = { MediaStore.Images.Media.DATA }; // 好像是android多媒体数据库的封装接口,具体的看Android文档 Cursor cursor = managedQuery(originalUri, proj, null, null, null); // 按我个人理解 这个是获得用户选择的图片的索引值 int column_index = cursor .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); // 将光标移至开头 ,这个很重要,不小心很容易引起越界 cursor.moveToFirst(); // 最后根据索引值获取图片路径 String path = cursor.getString(column_index); Uri uri = Uri.fromFile(new File(path)); mUploadMessage.onReceiveValue(uri); } catch (IOException e) { Log.e("TAG-->Error", e.toString()); } } } 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); progressBar.setVisibility(View.GONE); } } // flipscreen not loading again @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); } // To handle "Back" key press event for WebView to go back to previous // screen. /* * @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if * ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) { web.goBack(); * return true; } return super.onKeyDown(keyCode, event); } */ }
上述内容就是Android项目中如何在webview页面中上传文件,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。