可以对接与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