Old man needs to create a project recently. He needs to call some webpages in the server segment to select files. At the beginning, the project is quite tangled. I don't know where to start. On the internet, he roughly previewed the path they have taken, the traditional method is to use the following code:
Public void openfilechooser (valuecallback <URI> uploadmsg, string accepttype, string capture) {muploadmessage = uploadmsg; intent = new intent (intent. action_get_content); intent. addcategory (intent. category_openable); intent. settype ("image/*"); context. startactivityforresult (intent. createchooser (intent, "must be used to complete operations"), webmainactivity. filechooser_resultcode );}
After reading some online friends, basically the same code is used. All these are spider crawls others' documents, which is incredibly disgusting! I am so annoying that the search results I want to get have been overwritten by a large amount of garbage. However, I found the desired code from http://stackoverflow.com/. then, I was quite disappointed with some of the program blogs ....
Well, the Code mentioned above does not satisfy our needs at all. At least our product manager's general idea is that we can select images from cameras or photo albums, this is their core requirement. Therefore, we must customize
However, before uploading this file, let's talk about a few tips;
If you use webview, will you open the browser of the system inexplicably!
In fact, we need to customize our own things-webviewclient. In fact, what is defined is also good.
/***** Custom webviewclient. Otherwise, it will automatically jump to the * @ author spring sky * creation time of the System Browser: Aug 19,201 33: 40: 18 pm */private class mywebviewclient extends webviewclient {private context mcontext; Public mywebviewclient (context) {super (); mcontext = context ;}@ overridepublic void onpagestarted (webview view, string URL, bitmap favicon) {log. D (TAG, "url address:" + URL); super. onpagestarted (view, URL, favicon) ;}@ overridepublic void onpagefinished (webview view, string URL) {log. I (TAG, "onpagefinished"); super. onpagefinished (view, URL );}}
Then, call our custom webviewclient:
mWebView.setWebViewClient(new MyWebViewClient(this));
Now let's start to talk about the pop-up box. according to the instructions of stackoverflow, the HTML tag <input type = File> of the android source code is actually a custom webchromeclient, so we need to handle this, but one thing is that because the Android system has been upgraded (Google is pursuing perfection), many functions are implemented gradually, this is the compatibility issue. It should be noted that this does not affect our pop-up box. First, we need to customize our mywebchromeclient.
The Code is as follows:
/***** Customize webchromeclient and select Image Processing * @ author spring sky * Creation Time: Aug 19,201 33: 40: 46 pm */private class mywebchromeclient extends webchromeclient {// for Android 3.0 + public void openfilechooser (valuecallback <URI> uploadmsg, string accepttype) {If (muploadmessage! = NULL) return; muploadmessage = uploadmsg; selectimage ();} // 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 );}}
The above code is compatible with all the current Android Market versions. It should be noted that: selectimage (); is a method defined by me, the content is to pop up a selection box, I think there is no need to talk about album and camera. I want to share code with you, many of my friends reported that the program crashed when I selected an album or camera for the code I wrote. This time, I completely solved this problem and solved the version compatibility problem, annoying ..... programmer's troubles .... sorry ....
On the server side, I use struts upload of javaee. For details, refer:
1. I used a apache-tomcat7 as my server, everyone knows to upload files, upload will first send the client files stored in temporary files, after the upload is complete, the file will be copied to the official file, and then the temporary file will be deleted. As for why, if you are a more thoughtful person, you will understand why, if you do not know why, I hope you will think more about why the human brain is not used to fall in love and make money. It is used for thinking. I really don't understand why. Please contact me after thinking about it! QQ: 840950105
My computer is a MAC system, so the following code is configured in struts. xml:
<constant name="struts.multipart.saveDir" value="/Users/mac/Documents/tomcat/apache-tomcat-7.0.42/temp"/>
I hope you will pay attention to this, because many people have asked me why I always fail to upload files, so we need to configure the temporary directory first. If it is a window system, /users/MAC/documents/
In this directory, you guys and younger siblings really hope that you will pay more attention to every key point that others say when learning the program, and find out more errors and solve the problem by yourself, in this way, you can get out of your path!
2. A little bit of code, if a little bit of code, will understand, the request stores two values, one Upload message, one upload file path! (By the way, my javaee has not been tossing for many years, and I have forgotten about it. If you write anything on the server side, please forgive me. The programmer cannot afford to hurt me, two years later, I went home to farm... the product manager tortured me... test and torture me... customers of NIMA can torture me if they don't understand what they don't understand ...)
ServletActionContext.getRequest(). setAttribute("typeError",result);ServletActionContext.getRequest().setAttribute("uploadFile", rsImage);
The above are basically the core technical points. No more!
It should be noted that when you get the code of my android client, please first change the URL of your webview to the project URL of your own javaee deployment. Remember to remember, otherwise, old man is dying of vomiting blood!
I hate blogs and websites that use spider crawlers. I hope these Webmasters can live their own lives, don't pretend to be arrogant with others' experience! The road is self-built. Don't eat what others pull down and shout!
Copyright: spring sky
The sharing code is as follows:
Android client code
Java EE server code
By the way, I am not too young. In order to do my own things, I have some entrepreneurial ideas. If a startup friend lacks a loyal partner, please contact me! (Focus on Android iOS development. I don't have the same IQ as my God, but I have an unyielding mind. I don't bow down for difficulties, but I work hard for my ideals)