If not have a camera in device, showing black background color on android

chohyunwook

Recently I used cctv camera app but if not have a camera in device, showing this logcat and app is died

java.lang.RuntimeException: Unable to start activity ComponentInfo{kr.co.iosystem.blackeyeonandroid/kr.co.iosystem.blackeyeonandroid.BlackEyeActivity}: java.lang.NullPointerException
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
    at android.app.ActivityThread.access$800(ActivityThread.java:135)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5001)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
    at org.webrtc.PeerConnectionFactory.createVideoSource(PeerConnectionFactory.java:111)
    at kr.co.iosystem.blackeyeonandroid.BlackEyeActivity.onCreate(MainActivity.java:235)
    at android.app.Activity.performCreate(Activity.java:5231)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 
    at android.app.ActivityThread.access$800(ActivityThread.java:135) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:136) 
    at android.app.ActivityThread.main(ActivityThread.java:5001) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 
    at dalvik.system.NativeStart.main(Native Method) 

and my source MainActivity.java

@Override
protected void onCreate(final Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   String nameOfFrontFacingDevice = VideoCapturerAndroid.getNameOfFrontFacingDevice();
   String nameOfBackFacingDevice = VideoCapturerAndroid.getNameOfBackFacingDevice();
   VideoCapturerAndroid capturer = VideoCapturerAndroid.create(nameOfFrontFacingDevice);
   . 
   . 
   . 
   }

if not have a camera. capturer this return null so , I test

if (capturer == null || capturer.equals("") == true) {
   try {
      rebootProcess = Runtime.getRuntime().exec(new String[]{"su", "-c", "reboot"});
   } catch (IOException e) {
      e.printStackTrace();
    }
 }

and I execute app. I rebooted my device but I want if not have a camera, showing black color background

If its connected to camera then it showing GLSurfaceView

glview = (GLSurfaceView) findViewById(R.id.glview);
VideoRendererGui.setView(glview, null) ;
try {
     . . . .
} catch {
}

perhaps, I use fragment? please advice for me. I think

if (capturer == null || capturer.equals("") == true) {
   // showing black background ??..
  } 

but I don't know showing black background part.

thanks.

@update

MainActivity.java (full oncreate)

@Override
protected void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String nameOfFrontFacingDevice = VideoCapturerAndroid.getNameOfFrontFacingDevice();

    if (nameOfFrontFacingDevice != null) {
        VideoCapturerAndroid capturer = VideoCapturerAndroid.create(nameOfFrontFacingDevice);

        MediaConstraints videoConstraints = new MediaConstraints();
        VideoSource videoSource = peerConnectionFactory.createVideoSource(capturer, videoConstraints);
        localVideoTrack = peerConnectionFactory.createVideoTrack(VIDEO_TRACK_ID, videoSource);

        glview =(GLSurfaceView) findViewById.(R.id.showing);
        VideoRendererGui.setView(glview, null);
        try {
            renderer = VideoRendererGui.createGui(0,0,100,100, VideoRendererGui.ScalingType.SCALE_ASPECT_FILL, true);
            localVideoTrack.addRenderer(renderer);
        } catch (Exception e) {
             e.printStackTrace();
        }

        mediaStream = peerConnectionFactory.createLocalMediaStream(LOCAL_MEDIA_STREAM_ID);
        mediaStream.addTrack(localVideoTrack);
    } else {
        //space
    }

I changed my source your advice. but occur nullpointerException

In else part. how can I programmatically?

Shaishav

If its an app where you are using camera then, the app should not install in phones that lack this feature. In your manifest put:

<uses-feature android:name="android.hardware.camera"
    required="true" />

[EDIT]

According to VideoCapturerAndroid source, the following line will return null if no camera exist:

String nameOfFrontFacingDevice = VideoCapturerAndroid.getNameOfFrontFacingDevice();

So, in your case this must itself be coming null. However, you are passing the name to create. I think this is where NPE is coming from:

VideoCapturerAndroid capturer = VideoCapturerAndroid.create(nameOfFrontFacingDevice); // name might be null

So, you should put up a null check here:

if (nameOfFrontFacingDevice != null) {
    VideoCapturerAndroid capturer = VideoCapturerAndroid.create(nameOfFrontFacingDevice);
} else {
    // Other stuff
}

[EDIT 2]

For devices with no camera, your capturer would still be coming out null and that is creating the problem with call to peerConnectionFactory.createVideoSource(capturer, videoConstraints). Hence, we can make sure we call it only if we have the camera and avoid the NPE. Now, we run into another problem in glview.onResume() call in activity onResume(). So, we must initialise that before we check for camera. Check the code below for suggested fix:

        String nameOfFrontFacingDevice = VideoCapturerAndroid.getNameOfFrontFacingDevice();
        String nameOfBackFacingDevice = VideoCapturerAndroid.getNameOfBackFacingDevice();
        Log.i(TAG, "VideoCapturerAndroid.getNameOfFrontFacingDevice() = " + nameOfFrontFacingDevice);
        Log.i(TAG, "VideoCapturerAndroid.getNameOfBackFacingDevice() = " + nameOfBackFacingDevice);
        VideoCapturerAndroid capturer = VideoCapturerAndroid.create(nameOfFrontFacingDevice);

        // Initialising the glview here
        glview = (GLSurfaceView) findViewById(R.id.glview);
        VideoRendererGui.setView(glview, null);

        MediaConstraints videoConstraints = new MediaConstraints();
        if (capturer == null || capturer.equals("")) {
            Log.d(TAG, "not camera");

        }
        // Doing further processing only if capturer is not null
        else {
            VideoSource videoSource = peerConnectionFactory.createVideoSource(capturer, videoConstraints);

            localVideoTrack = peerConnectionFactory.createVideoTrack(VIDEO_TRACK_ID, videoSource);

            try {
                renderer = VideoRendererGui.createGui(0, 0, 100, 100, VideoRendererGui.ScalingType.SCALE_ASPECT_FILL, true);
                renderer_sub = VideoRendererGui.createGui(72, 72, 25, 25, VideoRendererGui.ScalingType.SCALE_ASPECT_FILL, true);
                localVideoTrack.addRenderer(renderer_sub);
                localVideoTrack.addRenderer(renderer);
            } catch (Exception e) {
                e.printStackTrace();
            }

            mediaStream = peerConnectionFactory.createLocalMediaStream(LOCAL_MEDIA_STREAM_ID);
            mediaStream.addTrack(localVideoTrack);

            ImageButton imageButton = (ImageButton) findViewById(R.id.backbutton);
            imageButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    moveTaskToBack(true);
                    finish();
                    android.os.Process.killProcess(android.os.Process.myPid());
                }
            });
        } 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

UISearchBar background image showing black color on edges

From Dev

Android device is not showing the background image

From Java

iOS app icon with transparent background showing black background on device

From Dev

Black color is not visible when a overlay also have black background with opacity

From Dev

Black color is not visible when a overlay also have black background with opacity

From Dev

WebRTC Black screen while flipping the camera on Android device

From Java

OpenCV app - Android camera keeps showing black screen

From Dev

Camera controls not showing on device

From Dev

Camera controls not showing on device

From Dev

android cordova-plugin-camera adds black background on PNG

From Dev

android cordova-plugin-camera adds black background on PNG

From Dev

HTML5 video background in Android showing black

From Dev

HTML5 video background in Android showing black

From Dev

Black color comes instead a transparent background for textview in android

From Dev

Android how to change the black default background color of the app

From Dev

body background color not showing?

From Dev

UIView Background Color Always Black

From Dev

Minicom black background color is not respected

From Dev

CGContextClearRect causing background color to be black

From Dev

clearColor make background color black

From Dev

XAML Background Color Black at Runtime

From Dev

Minicom black background color is not respected

From Dev

clearColor make background color black

From Dev

Camera screen showing only blank black image

From Dev

Android Camera (photos not showing)

From Dev

Android Camera in Background OpenCV

From Dev

Android background camera preview

From Dev

SVG clipPath showing black background with transparent fill

From Dev

Android AlertDialog produces black text with black background

Related Related

  1. 1

    UISearchBar background image showing black color on edges

  2. 2

    Android device is not showing the background image

  3. 3

    iOS app icon with transparent background showing black background on device

  4. 4

    Black color is not visible when a overlay also have black background with opacity

  5. 5

    Black color is not visible when a overlay also have black background with opacity

  6. 6

    WebRTC Black screen while flipping the camera on Android device

  7. 7

    OpenCV app - Android camera keeps showing black screen

  8. 8

    Camera controls not showing on device

  9. 9

    Camera controls not showing on device

  10. 10

    android cordova-plugin-camera adds black background on PNG

  11. 11

    android cordova-plugin-camera adds black background on PNG

  12. 12

    HTML5 video background in Android showing black

  13. 13

    HTML5 video background in Android showing black

  14. 14

    Black color comes instead a transparent background for textview in android

  15. 15

    Android how to change the black default background color of the app

  16. 16

    body background color not showing?

  17. 17

    UIView Background Color Always Black

  18. 18

    Minicom black background color is not respected

  19. 19

    CGContextClearRect causing background color to be black

  20. 20

    clearColor make background color black

  21. 21

    XAML Background Color Black at Runtime

  22. 22

    Minicom black background color is not respected

  23. 23

    clearColor make background color black

  24. 24

    Camera screen showing only blank black image

  25. 25

    Android Camera (photos not showing)

  26. 26

    Android Camera in Background OpenCV

  27. 27

    Android background camera preview

  28. 28

    SVG clipPath showing black background with transparent fill

  29. 29

    Android AlertDialog produces black text with black background

HotTag

Archive