In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
Today, the editor will share with you the relevant knowledge points about how to upload files using WebView in Android. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's learn about it.
1. Receive notification from WebView to open file selector
two。 After receiving the notification, open the file selector and wait for the user to select the file to be uploaded
3. Get the Uri of the file selected by the user in onActivityResult
4. Then pass the Uri to Html5
In this way, I have completed the process of selecting files for H5. Next, I will post the code for self-study.
First of all, WebView must support JS interaction, so turn on JS interaction
MWebView.getSettings () setJavaScriptEnabled (true)
When H5 calls the Api for uploading files, WebView will call back the openFileChooser and onShowFileChooser methods to inform us that what we need to do at this time is to override this method.
It should be noted that this method will call back different row parameter methods on different Api
MWebView.setWebChromeClient (new WebChromeClient () {@ Override public void onProgressChanged (WebView view, int newProgress) {if (newProgress = = 100) {mBar.setVisibility (View.GONE);} else {mBar.setVisibility (View.VISIBLE); mBar.setProgress (newProgress);} super.onProgressChanged (view, newProgress);} / / For Android API
< 11 (3.0 OS) public void openFileChooser(ValueCallback valueCallback) { uploadMessage = valueCallback; openImageChooserActivity(); } //For Android API >= 11 (3.0 OS) public void openFileChooser (ValueCallback valueCallback, String acceptType, String capture) {uploadMessage = valueCallback; openImageChooserActivity ();} / / For Android API > = 21 (5.0 OS) @ Override public boolean onShowFileChooser (WebView webView, ValueCallback filePathCallback, WebChromeClient.FileChooserParams fileChooserParams) {uploadMessageAboveL = filePathCallback; openImageChooserActivity (); return true;}})
We first save the callback object of ValueCallback in the openFileChooser method, which is finally used to inform H5 file address, and then we call the openFileChooser method to open the file selector.
Private void openImageChooserActivity () {Intent I = new Intent (Intent.ACTION_GET_CONTENT); i.addCategory (Intent.CATEGORY_OPENABLE); i.setType ("image/*"); startActivityForResult (Intent.createChooser (I, "ImageChooser"), FILE_CHOOSER_RESULT_CODE);}
When the user has selected the file, the onActivityResult method is called, and we rewrite and wait for the callback
@ Override protected void onActivityResult (int requestCode, int resultCode, Intent data) {super.onActivityResult (requestCode, resultCode, data); if (requestCode = = FILE_CHOOSER_RESULT_CODE) {if (null = = uploadMessage & & null = = uploadMessageAboveL) return; Uri result = data = = null | resultCode! = RESULT_OK? Null: data.getData (); if (uploadMessageAboveL! = null) {onActivityResultAboveL (requestCode, resultCode, data);} else if (uploadMessage! = null) {uploadMessage.onReceiveValue (result); uploadMessage = null;} @ TargetApi (Build.VERSION_CODES.LOLLIPOP) private void onActivityResultAboveL (int requestCode, int resultCode, Intent intent) {if (requestCode! = FILE_CHOOSER_RESULT_CODE | | uploadMessageAboveL = null) return; Uri [] results = null If (resultCode = = Activity.RESULT_OK) {if (intent! = null) {String dataString = intent.getDataString (); ClipData clipData = intent.getClipData (); if (clipData! = null) {results = new Uri [clipData.getItemCount ()]; for (int I = 0; I < clipData.getItemCount (); iTunes +) {ClipData.Item item = clipData.getItemAt (I); results [I] = item.getUri () }} if (dataString! = null) results = new Uri [] {Uri.parse (dataString)};}} uploadMessageAboveL.onReceiveValue (results); uploadMessageAboveL = null;} these are all the contents of the article "how to upload files using WebView in Android". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.