Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6810987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:16:19+00:00 2026-05-26T20:16:19+00:00

I am trying to invoke camera from HTML5 and JavaScript. My code is as

  • 0

I am trying to invoke camera from HTML5 and JavaScript. My code is as below. But I am getting null pointer exception.

Main Activity :

 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");  
            }  
        }  

Camera call :

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 page :

 <!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>  

Permissions in manifest:

<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)  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:16:20+00:00Added an answer on May 26, 2026 at 8:16 pm

    You are starting Camera Activity from OpenCamera Activity which is not yet started (according to android way of starting Activity), So the context of OpenCamera Activity is null, that is why your Context is null at that time and causing NullPointerException.

    Workaround:

    Pass a context to the Function startCameraActivity() as

    public void startCameraActivity(Context con){  
            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE );  
            con.startActivityForResult(intent,0);  
    }
    

    pass a context to the WebView

    webview.addJavascriptInterface(this,"context");
    

    and in your script

    <script type="text/javascript">  
        function openCamera(){   
            camera.startCameraActivity(context);  
        }  
    </script>
    

    OpenCamera need not to be an Activity

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im trying to invoke a method in java from javascript, but this doesn't happen
I'm trying to invoke a built-in activity from my custom CodeActivity for the TFS2010
im trying to invoke a specific class from within a jar file but I'm
I'm currently trying to invoke a method made in C from C# C code
I'm trying to invoke a native Windows command from Cygwin using run , but
I am trying to invoke my main activity's onKeyDown() with KEYCODE_BACK, so that it
So I'm trying to invoke a pointer to a function, but I'm failing. Could
Am trying to invoke a public web-service using a java client but am getting
I'm trying to invoke the below but the EventHander is not compatible with the
I am trying to invoke an ANT target from Windows (right-click) file context menu.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.