Android development experience-webpage calls Android image or file selection through webview

Source: Internet
Author: User

Some time ago, due to customer requirements, a client was used to combine web microsite applications. Here, this application is designed to modify the profile picture, but this page is implemented on the micro-website, which means that the website needs to call the Android method to open files, how is this implemented through webview? After discussion with colleagues on the server, I found that the methods are the same as those on the pc. They call an attribute called input type = file, so I started to look for it, how does webview respond to this attribute. As a result, it is not difficult to find information on the website. To use php to call the method to open a file, webview will rewrite a method named openFileChooser. However, this method is not easy to use. This method is to call the setWebChromeClient method of webview and then rewrite a WebChromeClient class. At this step, it is not difficult for developers to solve the problem. The key to the problem is that when you rewrite the WebChromeClient class, you will find that there is no openFileChooser method at all. How can you rewrite it? Does it mean that this method does not actually work? So I checked the data again and found that this method was actually a hidden method, and there was no explicit inheritance and rewriting relationship. Finally, I found that to use this method, I had to inherit the WebChromeClient class and write the openFileChooser (ValueCallback <Uri> uploadFile) method. The Code is as follows: abstract class extends {private WebChromeClient progress; protected publish (WebChromeClient wrappedClient) {progress = wrappedClient;}/** {@ inheritDoc} */@ Override public void onProgressChanged (WebView, int newProgress) {mWrappedC Lient. onProgressChanged (view, newProgress);}/** {@ inheritDoc} */@ Override public void onReceivedTitle (WebView view, String title) {mWrappedClient. onReceivedTitle (view, title);}/** {@ inheritDoc} */@ Override public void onReceivedIcon (WebView view, Bitmap icon) {mWrappedClient. onReceivedIcon (view, icon);}/** {@ inheritDoc} */@ Override public void onReceivedTouchIconUrl (WebView view, String Url, boolean precomposed) {mWrappedClient. onReceivedTouchIconUrl (view, url, precomposed);}/** {@ inheritDoc} */@ Override public void onShowCustomView (View view, CustomViewCallback callback) {mWrappedClient. onShowCustomView (view, callback);}/** {@ inheritDoc} */@ Override public void onHideCustomView () {mWrappedClient. onHideCustomView ();}/** {@ inheritDoc} */@ Override public boolean onCrea TeWindow (WebView view, boolean dialog, boolean userGesture, Message resultMsg) {return mWrappedClient. onCreateWindow (view, dialog, userGesture, resultMsg);}/** {@ inheritDoc} */@ Override public void onRequestFocus (WebView view) {mWrappedClient. onRequestFocus (view);}/** {@ inheritDoc} */@ Override public void onCloseWindow (WebView window) {mWrappedClient. onCloseWindow (window);}/** {@ inherit Doc} */@ Override public boolean onJsAlert (WebView view, String url, String message, JsResult result) {return mWrappedClient. onJsAlert (view, url, message, result);}/** {@ inheritDoc} */@ Override public boolean onJsConfirm (WebView view, String url, String message, JsResult result) {return mWrappedClient. onJsConfirm (view, url, message, result);}/** {@ inheritDoc} */@ Override public boolean onJ SPrompt (WebView view, String url, String message, String defaultValue, JsPromptResult result) {return mWrappedClient. onJsPrompt (view, url, message, defaultValue, result);}/** {@ inheritDoc} */@ Override public boolean onJsBeforeUnload (WebView view, String url, String message, jsResult result) {return mWrappedClient. onJsBeforeUnload (view, url, message, result);}/** {@ inheritDoc} */@ Override p Ublic void onExceededDatabaseQuota (String url, String databaseIdentifier, long currentQuota, long estimatedSize, long totalUsedQuota, WebStorage. quotaUpdater quotaUpdater) {mWrappedClient. encode (url, databaseIdentifier, currentQuota, estimatedSize, totalUsedQuota, quotaUpdater);}/** {@ inheritDoc} */@ Override public void encode (long spaceNeeded, long totalUsed Quota, WebStorage. quotaUpdater quotaUpdater) {mWrappedClient. onReachedMaxAppCacheSize (spaceNeeded, totalUsedQuota, quotaUpdater);}/** {@ inheritDoc} */@ Override public void encode (String origin, GeolocationPermissions. callback callback) {mWrappedClient. onGeolocationPermissionsShowPrompt (origin, callback);}/** {@ inheritDoc} */@ Override public void onGeolocationPe RmissionsHidePrompt () {mWrappedClient. onGeolocationPermissionsHidePrompt ();}/** {@ inheritDoc} */@ Override public boolean onJsTimeout () {return mWrappedClient. onJsTimeout ();}/** {@ inheritDoc} */@ Override @ Deprecated public void onConsoleMessage (String message, int lineNumber, String sourceID) {mWrappedClient. onConsoleMessage (message, lineNumber, sourceID);}/** {@ inheritDoc} */@ Override Public boolean onConsoleMessage (ConsoleMessage consoleMessage) {return mWrappedClient. onConsoleMessage (consoleMessage);}/** {@ inheritDoc} */@ Override public Bitmap getdefavidevideoposter () {return mWrappedClient. getdefavidevideoposter ();}/** {@ inheritDoc} */@ Override public View getVideoLoadingProgressView () {return mWrappedClient. getVideoLoadingProgressView ();}/** {@ inheritDoc} */@ Overr Ide public void getVisitedHistory (ValueCallback <String []> callback) {mWrappedClient. getVisitedHistory (callback);}/** {@ inheritDoc} */public void openFileChooser (ValueCallback <Uri> uploadFile) {(TestWebChromeClient) mWrappedClient ). openFileChooser (uploadFile) ;}} I found the above Code on eoe, not my own details. But I don't know who the original is, because you only need to Baidu openFileChooser to find this method. When you write it like this, it's time to set WebChromeClient. The setting method is as follows: mContentView. web_main_web.setWebChromeClient (new TestWebChromeClient (new WebChromeClient () {public void openFileChooser (ValueCallback <Uri> uploadFile) {if! = Null) return; mUploadMessage = uploadFile; // write your own method for calling the image. Here I encapsulate the method for calling the image. // The key point is the mUploadMessage parameter, after obtaining the image, you can pass this parameter back. // Specific usage: Baidu. Select_Activity.start (mContext, mUploadMessage, indexUrlString, FILE_SELECTED) ;}}); after you set it, you should test it happily. So you may find out happily, no !! No response! Are you beginning to suspect that this method is difficult? Are you trying to scold the hacker? Come and come, don't get angry. Let me tell you the truth. I will tell you, this method is actually not suitable for anyone at all. It's really okay, ~ This is effective on sdks earlier than 3.0... but now the mainstream Android machines are basically more than 4.0. Where can there be systems earlier than 3.0? So you started a new round of Niang. Why do I know? That's why I responded at that time! At that time, I tried to flip the information, and finally found the reason why Baidu, eoe, CSDN, Dewen, blog Park, Android bus, and DEVDIV were all broken, in the past, more than one parameter would have to be added for 3.0 of the mud coal to take effect! As a result, I stupidly followed the openFileChooser method rewritten by others and added an openFileChooser (ValueCallback <Uri> uploadFile, String acceptType) method to the TestWebChromeClient class. (I am not very clear about the specific usage of this acceptType parameter. If you know it, please let me know.) a corresponding call method is also added to the setWebChromeClient method of webview. So a new round of testing started again. Finally, once again, you were so excited that it would not work! (Po master: Hello! Don't try it out. You won't be able to share the technology if you're so stupid !) As a result, I finally believed that domestic search engines and forums were unreliable, And I relied on Google and stackoverflow. To be honest, the po master's English is very bad, and it is so bad that only the fifth grade of primary school (po Master started learning English at that time) so I don't want to depend on a foreign website if I don't want to, but I really can't see it. I can't search for it! I found Google for a full day, and finally found the same problem on stackoverflow through Google (my English level can only be used by Google and my face is covered, in Nima Versions later than 4.0, another parameter is required. You can add an openFileChooser (ValueCallback <Uri> uploadFile, String acceptType, String capture) method. Next I will paste the complete code of TestWebChromeClient. Abstract class extends {private WebChromeClient progress; protected publish (WebChromeClient wrappedClient) {progress = wrappedClient;}/** {@ inheritDoc} */@ Override public void onProgressChanged (WebView, int newProgress) {mWrappedClient. onProgressChanged (view, newProgress);}/** {@ inheritDoc} */@ Override public void onReceivedT Itle (WebView view, String title) {mWrappedClient. onReceivedTitle (view, title);}/** {@ inheritDoc} */@ Override public void onReceivedIcon (WebView view, Bitmap icon) {mWrappedClient. onReceivedIcon (view, icon);}/** {@ inheritDoc} */@ Override public void onReceivedTouchIconUrl (WebView view, String url, boolean precomposed) {mWrappedClient. onReceivedTouchIconUrl (view, url, precomposed );}/**{@ InheritDoc} */@ Override public void onShowCustomView (View view, CustomViewCallback callback) {mWrappedClient. onShowCustomView (view, callback);}/** {@ inheritDoc} */@ Override public void onHideCustomView () {mWrappedClient. onHideCustomView ();}/** {@ inheritDoc} */@ Override public boolean onCreateWindow (WebView view, boolean dialog, boolean userGesture, Message resultMsg) {return mWrappedClie Nt. onCreateWindow (view, dialog, userGesture, resultMsg);}/** {@ inheritDoc} */@ Override public void onRequestFocus (WebView view) {mWrappedClient. onRequestFocus (view);}/** {@ inheritDoc} */@ Override public void onCloseWindow (WebView window) {mWrappedClient. onCloseWindow (window);}/** {@ inheritDoc} */@ Override public boolean onJsAlert (WebView view, String url, String message, JsResult result) {Return mWrappedClient. onJsAlert (view, url, message, result);}/** {@ inheritDoc} */@ Override public boolean onJsConfirm (WebView view, String url, String message, JsResult result) {return mWrappedClient. onJsConfirm (view, url, message, result);}/** {@ inheritDoc} */@ Override public boolean onJsPrompt (WebView view, String url, String message, String defaultValue, jsPromptResult result) {retu Rn mWrappedClient. onJsPrompt (view, url, message, defaultValue, result);}/** {@ inheritDoc} */@ Override public boolean onJsBeforeUnload (WebView view, String url, String message, jsResult result) {return mWrappedClient. onJsBeforeUnload (view, url, message, result);}/** {@ inheritDoc} */@ Override public void onExceededDatabaseQuota (String url, String databaseIdentifier, long currentQuota, long es TimatedSize, long totalUsedQuota, WebStorage. quotaUpdater quotaUpdater) {mWrappedClient. encode (url, databaseIdentifier, currentQuota, estimatedSize, totalUsedQuota, expiration);}/** {@ inheritDoc} */@ Override public void encode (long spaceNeeded, long totalUsedQuota, WebStorage. quotaUpdater quotaUpdater) {mWrappedClient. onReachedMaxAppCacheSize (spaceNeeded, TotalUsedQuota, quotaUpdater);}/** {@ inheritDoc} */@ Override public void onGeolocationPermissionsShowPrompt (String origin, GeolocationPermissions. callback callback) {mWrappedClient. onGeolocationPermissionsShowPrompt (origin, callback);}/** {@ inheritDoc} */@ Override public void onGeolocationPermissionsHidePrompt () {mWrappedClient. onGeolocationPermissionsHidePrompt ();}/** {@ inheritDoc }*/ @ Override public boolean onJsTimeout () {return mWrappedClient. onJsTimeout ();}/** {@ inheritDoc} */@ Override @ Deprecated public void onConsoleMessage (String message, int lineNumber, String sourceID) {mWrappedClient. onConsoleMessage (message, lineNumber, sourceID);}/** {@ inheritDoc} */@ Override public boolean onConsoleMessage (lelemessage ConsoleMessage) {return mWrappedClient. onConsoleMess Age (consoleMessage);}/** {@ inheritDoc} */@ Override public Bitmap getdefavidevideoposter () {return mWrappedClient. getdefavidevideoposter ();}/** {@ inheritDoc} */@ Override public View getVideoLoadingProgressView () {return mWrappedClient. getVideoLoadingProgressView ();}/** {@ inheritDoc} */@ Override public void getVisitedHistory (ValueCallback <String []> callback) {mWrappedClient. getVisitedHisto Ry (callback);}/** {@ inheritDoc} */public void openFileChooser (ValueCallback <Uri> uploadFile) {(TestWebChromeClient) mWrappedClient ). openFileChooser (uploadFile);}/** {@ inheritDoc} */public void openFileChooser (ValueCallback <Uri> uploadFile, String acceptType) {(TestWebChromeClient) mWrappedClient ). openFileChooser (uploadFile, acceptType);}/** {@ inheritDoc} */public void openFileChooser (ValueCallback <Uri> uploadFile, String acceptType, String capture) {(TestWebChromeClient) mWrappedClient). openFileChooser (uploadFile, acceptType, capture);} the method to be added by the handler. Public void openFileChooser (ValueCallback <Uri> uploadFile, String acceptType) {openFileChooser (uploadFile);} public void openFileChooser (ValueCallback <Uri> uploadFile, String acceptType, String capture) {openFileChooser (uploadFile);} although many of the previous sections are not difficult to find, the hidden code in the section 3.0 and 4.0 is really terrible. I almost gave up At the beginning. I have not found this description in domestic forums, so I will share it here as a mark, saving my time and forgetting it. At last, I don't need to be called again. Thanks to CCAV, TVC, Dad, Mom, and foreigners. If you have any questions, please feel free to join me on Sina Weibo. You are welcome to discuss Android Application development issues.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.