0

I am a newbie to android development. I am making a simple app to open camera in sufaceView. I want to work this app from the android API level 16. I have written some code and that works in API level 22 emulator but not in API level 16 emulator (When I click on Start button, app crashes)

VideoServer.java

package com.example.admin2.cameraonsurfaceview;


import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class VideoServer extends Activity implements SurfaceHolder.Callback {
    TextView testView;

    Camera camera;
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;

    private final String tag = "VideoServer";

    Button start, stop;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        start = (Button)findViewById(R.id.btn_start);
        start.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View arg0) {
                start_camera();
            }
        });

        stop = (Button)findViewById(R.id.btn_stop);
        stop.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View arg0) {
                stop_camera();
            }
        });

        surfaceView = (SurfaceView)findViewById(R.id.surfaceView1);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        Toast.makeText(this, "Need your location!", Toast.LENGTH_SHORT).show();



    }

    private void start_camera() {
        try{
            camera = Camera.open();
        }catch(RuntimeException e){
            Log.e(tag, "init_camera: " + e);
            return;
        }
        Camera.Parameters param;
        param = camera.getParameters();
        //modify parameter
        param.setPreviewFrameRate(20);
        param.setPreviewSize(176, 144);
        camera.setParameters(param);
        try {
            camera.setPreviewDisplay(surfaceHolder);
            camera.startPreview();
        } catch (Exception e) {
            Log.e(tag, "init_camera: " + e);
            return;
        }
    }

    private void stop_camera() {
        camera.stopPreview();
        camera.release();
    }

    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
    }
}

and the layout file look like

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.admin2.cameraonsurfaceview.MainActivity">

    <SurfaceView
        android:id="@+id/surfaceView1"
        android:layout_width="201dp"
        android:layout_height="230dp"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        android:layout_marginStart="52dp"
        android:layout_marginEnd="52dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="25dp"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_marginLeft="52dp"
        android:layout_marginRight="52dp"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginBottom="8dp"
        app:layout_constraintVertical_bias="0.0" />

    <Button
        android:id="@+id/btn_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start"
        android:layout_marginTop="27dp"
        app:layout_constraintTop_toBottomOf="@+id/surfaceView1"
        android:layout_marginLeft="52dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:layout_marginStart="52dp" />

    <Button
        android:id="@+id/btn_stop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Stop"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintBottom_creator="1"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginEnd="52dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginBottom="16dp"
        android:layout_marginRight="131dp"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@+id/surfaceView1"
        app:layout_constraintVertical_bias="0.157" />
</android.support.constraint.ConstraintLayout>

My android manifest looks like

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.admin2.cameraonsurfaceview">

    <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="22" />

    <uses-feature android:name="android.hardware.camera" />
    <uses-permission android:name="android.permission.CAMERA" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".VideoServer">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

when I click on the start button, app stops and I get the following exception

10-05 15:39:48.174 2335-2335/com.example.admin2.cameraonsurfaceview E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
  at com.example.admin2.cameraonsurfaceview.VideoServer.start_camera(VideoServer.java:61)
  at com.example.admin2.cameraonsurfaceview.VideoServer.access$000(VideoServer.java:14)
  at com.example.admin2.cameraonsurfaceview.VideoServer$1.onClick(VideoServer.java:34)
  at android.view.View.performClick(View.java:4084)
  at android.view.View$PerformClick.run(View.java:16966)
  at android.os.Handler.handleCallback(Handler.java:615)
  at android.os.Handler.dispatchMessage(Handler.java:92)
  at android.os.Looper.loop(Looper.java:137)
  at android.app.ActivityThread.main(ActivityThread.java:4745)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:511)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
  at dalvik.system.NativeStart.main(Native Method)
Haem
  • 929
  • 6
  • 15
  • 31
Joyal
  • 2,587
  • 3
  • 30
  • 45

2 Answers2

0

Delete <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="22" /> from your manifest.

Then check your version into build.gradle. Example:

android { defaultConfig { minSdkVersion 16 // here you need to change into 16 if you already didn't targetSdkVersion 25 . . In order to work on API 16 you need to set minSdkVersion to 16. Then resync gradle and solve the problems that may appear with code that is not compatible with version 16.

Stoica Mircea
  • 782
  • 10
  • 22
0

Finally I fixed the issue myself. The following line of code was causing the NullPointerException

camera = Camera.open();

I changed to

camera = Camera.open(1);

then it worked.

Joyal
  • 2,587
  • 3
  • 30
  • 45