Android uses HTML5 as the interface, and JavaScript calls the camera instance

Source: Internet
Author: User

During Android development, it takes most of the time to create the interface, and only developers can make it by themselves. Therefore, I have been trying to use HTML5 + css3 to complete the process. phonegap is not quite mature yet, I am going to use Android native + webview to complete the process. I found that it is similar to flash as for mutual debugging. BelowCodeThe Native Interface is called through HTML JavaScript to complete the photo selection code. Click the image to select the image (from the camera or camera ). For the photo selection code, see the previous article.Article《Android photo and Cut Processing Method"

Activity Code:

Import Sh. Path. R;
Import Sh. Path. UI. image. imagecrop;
Import Android. App. activity;
Import Android. content. intent;
Import Android. content. PM. activityinfo;
Import Android. OS. Bundle;
Import Android. OS. Handler;
Import Android. View. view;
Import Android. View. window;
Import Android. WebKit. webview;
Import Android. widget. Toast;

public class htmlfeedhomeactivity extends activity {
private imagecrop photoutil;
private webview = NULL;
private handler mhandler = new handler ();
private string mjsdata = NULL;
private activity activitythis = This;

@ Override
/** Called when the activity is first created .*/
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Requestwindowfeature (window. feature_no_title );
Setcontentview(r.layout.html feeds );
Setupwebview ();
Photoutil = new imagecrop (this );
This. setrequestedorientation (activityinfo. screen_orientation_portrait );

}

Private void setupwebview (){

Webview = (webview) findviewbyid (R. Id. idhtmlfeedhome );
Webview. getsettings (). setjavascriptenabled (true );
Webview. setscrollbarstyle (view. scrollbars_inside_overlay );
Webview. loadurl ("file: // android_asset/path/index.html ");
Webview. addjavascriptinterface (New javascriptinterface (), "Caller ");
}
@ Override
Protected void onactivityresult (INT requestcode, int resultcode, intent data ){
Try {
If (photoutil. capituredimage (requestcode, resultcode, data )){
If (imagecrop. crop_image = requestcode ){
// Mimageview. setimageuri (photoutil. getimagecaptureuricutted ());
Webview. loadurl ("javascript: setphoto ('" + mjsdata + "', '" + photoutil. getimagecaptureuricutted (). getpath () + "')");
}
Return;
}
} Catch (exception exp ){
Exp. printstacktrace ();
}
}

Private class javascriptinterface {
@ Suppresswarnings ("UNUSED ")
Public void clickonandroid (){
Mhandler. Post (New runnable (){
Public void run (){
Webview. loadurl ("javascript: Wave ()");
}
});
}
@ Suppresswarnings ("UNUSED ")
Public void takephoto (string jsdata ){
Mjsdata = jsdata;
Photoutil. getdialog (). Show ();
}
@ Suppresswarnings ("UNUSED ")
Public void showtip (string tip ){
Toast. maketext (activitythis, tip, Toast. length_short). Show ();
}
}
}

For details, see "using HTML5 as the interface and using JavaScript to call a camera instance".

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.