可以對接與JavaScript Android原生代碼,以捕捉按鈕上的點擊,並有android代碼發送意圖打開cammera,拍攝照片,並將其存儲在某個路徑中。
然後,在onActivityResult你將獲得從路徑照片並上傳到Web服務器的照顧。要做到這一點的方法之一是位圖編碼在一個base64字符串,並使用Android HTTP客戶端在POST形式發送。
我將演示如何執行javscript和android之間的接口(這是quesiton的作用),關於操作foto的所有其他內容都有其自身的複雜性,並且有許多關於它的教程。
主要活動
public class MainActivity extends AppCompatActivity {
private WebView webView;
private Bitmap imageBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.addJavascriptInterface(new WebAppInterface(this),"Android");
webView.loadData(
"<Button id=\"btnTakePhoto\" onClick=\"takePhoto()\">TakePhoto</Button><div class=\"takePhoto\">\n" +
"<script type=\"text/javascript\">" +
"function takePhoto() {\n" +
" Android.takePhoto();\n" +
" }\n" +
"</script>" +
"</div>", "text/html","UTF-8");
}
public class WebAppInterface {
Context mContext;
/** Instantiate the interface and set the context */
WebAppInterface(Context c) {
mContext = c;
}
/** The android function calld from javascript */
@JavascriptInterface
public void takePhoto() {
dispatchTakePictureIntent();
}
}
/* This gests a Thumbnail only*/
static final int REQUEST_IMAGE_CAPTURE = 1;
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
imageBitmap = (Bitmap) extras.get("data");
/* Encode bitmap as base64 and transmit to server */
}
}
}
注意:這種接口的是不安全的上下面的Andriod 17.
EDIT 1
在HTML
<html>
<body>
<div class="takePhoto">
<Button onClick="takePhoto()">Take Photo </Button>
</div>
<script type="text/javascript">
function takePhoto(){
Android.takePhoto();
}
</script>
</body>
</html>
編輯2
在清單中,我只添加了:
<uses-feature android:name="android.hardware.camera"
android:required="true" />
對於您的應用程序,您還需要INTERNET權限。
是通過點擊此按鈕,文件瀏覽器打開成功....但沒有與相機有關的 – Kalkhouri