Android開發時候,介面製作基本會耗去大半部分時間,而且只能開發人員自己製作,所以一直想借用HTML5+CSS3完成,PhoneGap還不太成熟,自己準備用Android原生+WebView完成,發現和Flash As做互調差不多。以下代碼是通過HTML的javascript調用原生介面,完成照片選取的代碼,點擊映像,將選擇圖片(從相機或者網路攝影機)。照片選取代碼見上一篇文章 《Android 拍照加剪下處理方式》
Activity代碼:
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 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.htmlfeeds);
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();
}
}
}
詳見 《用Html5做介面,javascript調用網路攝影機執行個體》