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 3841570
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T15:40:17+00:00 2026-05-19T15:40:17+00:00

Have read the books and hints, got rid of all compile errors and warnings

  • 0

Have read the books and hints, got rid of all compile errors and warnings and put in some debug statements.

package com.cit.BroadcastSim;

import java.io.IOException;

import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class BroadcastActivity extends Activity implements SurfaceHolder.Callback {
  public Camera myCamera;
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.broadcast);  // Inflate the broadcast.xml file

    Log.d("BROADCAST", "Creating the Activity");
    SurfaceView cameraSurface = (SurfaceView)findViewById(R.id.camerasurface);
    SurfaceHolder cameraHolder = cameraSurface.getHolder();
    cameraHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    cameraHolder.addCallback(this);
    Log.d("BROADCAST", "Now wait for some CallBacks");
}


  public void surfaceCreated(SurfaceHolder holder) {
    // Surface created, now it is possible to set the preview
    Log.d("BROADCAST", "Surface Created");
    try {
      Log.d("BROADCAST","CAMERA: NOT NULL");
      myCamera = Camera.open();
      myCamera.setPreviewDisplay(holder);
      myCamera.startPreview();
      } catch (IOException e) {
        Log.d("BROADCAST", e.getMessage());
      }
  }

  public void surfaceDestroyed(SurfaceHolder holder) {
    Log.d("BROADCAST", "Surface Destroyed");
    myCamera.stopPreview();
    myCamera.release();
  }

  public void surfaceChanged(SurfaceHolder holder, int I, int J, int K) {
    Log.d("BROADCAST", "Surface Changed");
    myCamera.stopPreview();
    myCamera.release();
  }
}

In the DDMS debugger, I get a log message for ‘Creating the Activity’ followed by ‘Now wait for some CallBacks’ and nothing more in terms of my Debug messages so I think Callback is not working – for the life of me can’t see where I have got it wrong.

In the manifest I have

The Activity XML page has

<TextView  
  android:layout_width="fill_parent" 
  android:layout_height="wrap_content" 
  android:text="@string/Broadcast"
  />
<SurfaceView
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:id="@+id/camerasurface" 
  />
</LinearLayout>

Finally, on the Android phone (HTC Wildfire), the page loads, the textView message appears at the top left and that is all.

Should mention that I am very new to this platform and accept that I might have missed something very very basic.

Any ideas/comments will be very much appreciated,

Oliver

  • 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-19T15:40:17+00:00Added an answer on May 19, 2026 at 3:40 pm

    haven’t got enough time to explain (meeting with friends in a few minutes). I basically added some functions into “surfaceChanged()” (this is where you should start the preview).

    Therefore you don’t need mCam.startPreview() in “surfaceCreated()”

        public void surfaceCreated(SurfaceHolder holder) {
        // Surface created, now it is possible to set the preview
        Log.d("BROADCAST", "Surface Created");
        try {
          Log.d("BROADCAST","CAMERA: NOT NULL");
          mCam = Camera.open();
          mCam.setPreviewDisplay(holder);
          //myCamera.startPreview();
          } catch (IOException e) {
            Log.d("BROADCAST", e.getMessage());
          }
      }
    
      public void surfaceDestroyed(SurfaceHolder holder) {
        Log.d("BROADCAST", "Surface Destroyed");
        mCam.stopPreview();
        mCam.release();
      }
    
      public void surfaceChanged(SurfaceHolder holder, int I, int J, int K) {
          Camera.Parameters parameters = mCam.getParameters();
        List<Size> previewSizes = parameters.getSupportedPreviewSizes();
        Size bestSize = null;
        int bestDiff = 0;
        int diff = 0;
        for (Size size : previewSizes) {
                diff = Math.abs(K - size.height) + Math.abs(J - size.width);
            if (bestSize == null || diff < bestDiff) {
                    bestSize = size;
                    bestDiff = diff;
            }
            parameters.setPreviewSize(bestSize.width, bestSize.height);
            mCam.setParameters(parameters);
        }
    
        //start preview of camera
          mCam.startPreview();
      }
    

    xml-file (you did a little copy & paste error)

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <TextView  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="@string/hello"
        />
    

    Hope I could help 🙂
    I think, via setting the orientation, you could change the camera picture to portrait mode. 🙂 (it’s landscape right now)

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

Sidebar

Related Questions

I have read through several reviews on Amazon and some books seem outdated. I
I have read around 4-5 books on design patterns, but still I don't feel
I'm quite confused about several books in .NET that I have read. Would someone
I have read on Stack Overflow some people that have converting to C#2.0 to
I have the need to read the Thunderbird address book on the fly. It
I have read a lot that LISP can redefine syntax on the fly, presumably
I have read about partial methods in the latest C# language specification , so
I have read that using database keys in a URL is a bad thing
I have read this post about how to test private methods. I usually do
I have read (or perhaps heard from a colleague) that in .NET, TransactionScope can

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.