我正在尝试从HTML5和JavaScript调用相机。我的代码如下。但我得到空指针异常。
主要活动:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView webview=(WebView)findViewById(R.id.webkitWebView1);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
//load html5 page
webview.loadUrl("file:///android_asset/www/html/opencamera.html");
openCamera=new OpenCamera();
webview.addJavascriptInterface(openCamera,"camera");
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode== 0 && resultCode == Activity.RESULT_OK){
System.out.println("Done");
}
}
相机电话:
public class OpenCamera extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void startCameraActivity(){
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
startActivityForResult(intent,0);
}
}
html页面:
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
function openCamera(){
//alert("cameras");
camera.startCameraActivity();
}
</script>
<title>Welcome</title>
</head>
<body>
Hello World!!!
<br/>
<button type="button" onclick="openCamera()">Capture Image</button>
</body>
</html>
清单中的权限:
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.INTERNET" />
Logcat:
11-16 08:56:07.133: ERROR/AndroidRuntime(539): java.lang.NullPointerException
11-16 08:56:07.133: ERROR/AndroidRuntime(539): at android.app.Activity.startActivityForResult(Activity.java:2817)
11-16 08:56:07.133: ERROR/AndroidRuntime(539): at com.test.camera.OpenCamera.startCameraActivity(OpenCamera.java:17)
11-16 08:56:07.133: ERROR/AndroidRuntime(539): at android.webkit.WebViewCore.nativeTouchUp(Native Method)
11-16 08:56:07.133: ERROR/AndroidRuntime(539): at android.webkit.WebViewCore.nativeTouchUp(Native Method)
11-16 08:56:07.133: ERROR/AndroidRuntime(539): at android.webkit.WebViewCore.access$3300(WebViewCore.java:52)
11-16 08:56:07.133: ERROR/AndroidRuntime(539): at android.webkit.WebViewCore$EventHub$1.handleMessage(WebViewCore.java:1150)
11-16 08:56:07.133: ERROR/AndroidRuntime(539): at android.os.Handler.dispatchMessage(Handler.java:99)
11-16 08:56:07.133: ERROR/AndroidRuntime(539): at android.os.Looper.loop(Looper.java:123)
11-16 08:56:07.133: ERROR/AndroidRuntime(539): at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:621)
11-16 08:56:07.133: ERROR/AndroidRuntime(539): at java.lang.Thread.run(Thread.java:1096)
答案 0 :(得分:1)
你从Camera Activity
Activity开始OpenCamera
尚未启动的活动(根据android的方式启动Activity
),所以OpenCamera
Activity的上下文为空,这就是为什么您的Context
当时为null
并导致NullPointerException
。
解决方法:
将上下文传递给函数startCameraActivity()
as
public void startCameraActivity(Context con){
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );
con.startActivityForResult(intent,0);
}
将上下文传递给WebView
webview.addJavascriptInterface(this,"context");
并在您的脚本中
<script type="text/javascript">
function openCamera(){
camera.startCameraActivity(context);
}
</script>
OpenCamera
不一定是Activity
答案 1 :(得分:0)
您应该在清单文件中拥有正确的意图过滤器。这就是你得到异常的原因
答案 2 :(得分:0)
启动相机时试一试
Intent intent = new Intent("com.your.package.TAKE_PICTURE");
startActivityForResult(intent,0);
<activity android:name="OpenCamera" android:label="@string/app_name">
<intent-filter>
<action android:name="com.your.package.TAKE_PICTURE" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>