Android webview Environment Open camera, file management to upload

Source: Internet
Author: User
Tags filetime

WebView under the pit odd, directly under the browser can directly bring up the camera and file management, but in WebView below is not, in the online review of a lot of articles, have to say pit odd, up to finally pieced together to succeed,

Android version is 4.4.2, Debug Pass.

Android Key Source:

protected valuecallback<uri> muploadmessage;
protected int filechooser_resultcode = 1;
Private String Mcamerafilepath;

@SuppressLint ("Sdcardpath")
@Override
protected void Onactivityresult (int requestcode, int resultcode,
Intent Intent) {

if (null = = Muploadmessage)
Return
Uri result = Intent = = NULL | | ResultCode! = RESULT_OK? Null:intent.getData ();
if (result = = NULL && Intent = = NULL && ResultCode = = ACTIVITY.RESULT_OK) {
File Camerafile = new file (Mcamerafilepath);
if (camerafile.exists ()) {
result = Uri.fromfile (camerafile);
Sendbroadcast (New Intent (intent.action_media_scanner_scan_file, result));
}
}

}

This.wv.setWebChromeClient (New Webchromeclient () {

public void onprogresschanged (WebView view,int progress)
{
Trigger when loading progress changes
if (progress==100) {
Handler.sendemptymessage (1);//If all loads, hides the progress dialog box
}
Super.onprogresschanged (view, progress);
}

For Android 3.0+
public void Openfilechooser (valuecallback<uri> uploadmsg, String accepttype) {
if (muploadmessage! = null)
Return
Muploadmessage = uploadmsg;
Startactivityforresult (Createdefaultopenableintent (), Filechooser_resultcode);
}

For Android < 3.0
@SuppressWarnings ("unused")
public void Openfilechooser (valuecallback<uri> uploadmsg) {
Openfilechooser (Uploadmsg, "");
}

For Android > 4.1.1
@SuppressWarnings ("unused")
public void Openfilechooser (valuecallback<uri> uploadmsg, String accepttype, string capture) {
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 ("image/*");

Intent chooser = createchooserintent (Createcameraintent ());
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, "please select");
Return chooser;
}

Private Intent createcameraintent () {
Note: The main purpose of the code here is to save the photo file in the Browser-photos folder (not the system default folder)
If you don't need to do this, you can simplify the code

Intent cameraintent = new Intent (mediastore.action_image_capture);
File Externaldatadir = environment.getexternalstoragepublicdirectory (ENVIRONMENT.DIRECTORY_DCIM);
File Cameradatadir = new file (Externaldatadir.getpath () + "/images/");
Cameradatadir.mkdirs ();
Mcamerafilepath = Cameradatadir.getpath () + system.currenttimemillis () + ". jpg";
Cameraintent.putextra (Mediastore.extra_output, Uri.fromfile (New File (Mcamerafilepath)));
return cameraintent;
}

});

Web-side uploads:

var fd = new FormData ();
$ ("#UpPrompt"). HTML ("<span style= ' font-size:12px; ' > Upload in ...&nbsp;</span> ");
Fd.append ("Upfile", $ ("#UpFile"). Get (0). Files[0]);
$.ajax ({
URL: "Upload.aspx",
Type: "POST",
Processdata:false,
Contenttype:false,
DATA:FD,
Success:function (addr) {
if (addr! = "") {
var Imgtext = $ ("#DisplayImgList"). html ();
Imgtext = Imgtext + " '
$ ("#UpPrompt"). HTML ("");
$ ("#DisplayImgList"). HTML (Imgtext)
}
},
Error:function (XMLHttpRequest, Textstatus, Errorthrown) {
$ ("#UpPrompt"). html ("upload failed");
alert (Xmlhttprequest.responsetext);
}
});

Resolve the Upfile control change event to respond only once

var jqobj = $ ("#UpFile");
Jqobj.val ("");
var domobj = jqobj[0];
domobj.outerhtml = domobj.outerhtml;
var newjqobj = Jqobj.clone ();
Jqobj.before (Newjqobj);
Jqobj.remove ();
$ ("#UpFile"). Unbind (). Change (function (e) {
UploadFile ();
});

Web-side Server source code:

Using System;
Using System.Collections.Generic;
Using System.IO;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;

Namespace Web.custphone
{
public partial class Upload:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
if (Request.files[0]. FileName = "")
{
String filedir = "/uploadimg";
String fileName = "";
String destfilename = "";
String nfiledir = "";
String stradjunctfile = "";
String filetailname =
Filetailname = System.IO.Path.GetExtension (Request.files[0]. FileName);

Construct the name of the uploaded file
String fileTime = DateTime.Now.Year.ToString () + DateTime.Now.Month.ToString ()
+ DateTime.Now.Day.ToString () + DateTime.Now.Hour.ToString ()
+ DateTime.Now.Second.ToString () + DateTime.Now.Minute.ToString ()
+ DateTime.Now.Millisecond.ToString ();

FileName = FileTime + ". jpg";

Nfiledir = Filedir + "/" + DateTime.Now.Year.ToString () + "/" + DateTime.Now.Month.ToString (). PadLeft (2, ' 0 ');
if (Directory.Exists (Server.MapPath (request.applicationpath) + nfiledir) = = False)
{
Directory.CreateDirectory (Server.MapPath (request.applicationpath) + Nfiledir);
}
Stradjunctfile = Nfiledir + fileName;
destFileName = Server.MapPath (request.applicationpath) + stradjunctfile;
destFileName = Server.urldecode (destFileName);
Request.files[0]. SaveAs (destFileName);
Response.Write (Request.applicationpath + stradjunctfile.tostring ());
}
Else
{
Response.Write ("");
}
}
}
}

Thank you again for your online article, because the reference too much, this article Android content is forwarded.

Android webview Environment Open camera, file management to upload

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.