Camera Preview not working on my G2

by Stephen Lebed » Wed, 20 Oct 2010 13:15:12 GMT

Sponsored Links
 'm trying to get the camera preview running on my G2.

This is the code I'm testing with, which was taken from the sample
code. It is using supplemental code supplied by Wu-Cheng, but its
still throwing an error

* Copyright (C) 2007 The Android Open Source Project
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* See the License for the specific language governing permissions and
* limitations under the License.

package com.sltools;

import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import java.util.List;


public class CameraPreview extends Activity {
private Preview mPreview;

protected void onCreate(Bundle savedInstanceState) {

// Hide the window title.

// Create our Preview view and set it as the content of our
mPreview = new Preview(this);



class Preview extends SurfaceView implements SurfaceHolder.Callback {
SurfaceHolder mHolder;
Camera mCamera;

Preview(Context context) {

// Install a SurfaceHolder.Callback so we get notified when
// underlying surface is created and destroyed.
mHolder = getHolder();

public void surfaceCreated(SurfaceHolder holder) {
// The Surface has been created, acquire the camera and tell
it where
// to draw.
mCamera =;
try {
} catch (IOException exception) {
mCamera = null;
// TODO: add more exception handling logic here

public void surfaceDestroyed(SurfaceHolder holder) {
// Surface will be destroyed when we return, so stop the
// Because the CameraDevice object is not a shared resource,
it's very
// important to release it when the activity is paused.
mCamera = null;

public void surfaceChanged(SurfaceHolder holder, int format, int
w, int h) {
// Now that the size is known, set up the camera parameters and
// the preview.
Camera.Parameters parameters = mCamera.getParameters();

List<Size> sizes = parameters.getSupportedPreviewSizes();
Size optimalSize = getOptimalPrevi

Camera Preview not working on my G2

by TreKing » Wed, 20 Oct 2010 23:19:47 GMT


Does it not tell you which class was not found? That would be helpful :-)

TreKing <> ; - Chicago
transit tracking app for Android-powered devices


Sponsored Links

Camera Preview not working on my G2

by Stephen Lebed » Thu, 21 Oct 2010 12:27:53 GMT

 Hi TreKing,

Two things

1) The logcat says Runtime exception: Cannot instantiate activity
ComponentInfo.  I'm not sure what that is supposed to mean.  Maybe you
could shed some insight on this.
2) How does someone change the discussion subject?  I can't believe
someone get comes in and hijacks my thread.

Stephen Lebed


Camera Preview not working on my G2

by TreKing » Thu, 21 Oct 2010 22:49:07 GMT


Is there any more to the log cat? Post the 5 - 10 lines above and below that
error. Also look for "cause by" in the message and post the 5 - 10 lines
above and below THAT.

I think that's yet another bug with Google Groups. I'm reading this thread
in Gmail and it's currently just you and me :-)

TreKing <> ; - Chicago
transit tracking app for Android-powered devices


Other Threads

1. could not resolve android.view.View$OnClickListener

Hi all,

I tried out the tutorial code of different layout.

When I have :
import android.view.View$OnClickListener;

Eclipse gives me an error msg of "The import android.view.View
$OnClickListener" cannot be resolved.

I believe Google had overridden the way library is imported using "$".
So, eclipse is not picking up the android.jar and reading it

But I have the "preferences" > "android" > SDK location pointing to
the right dir.

Anything I am missing?

Thank a lot!

Jimmy Chu


2. why is bitmap .getWidth() and .getHeight() returning -1

im having trouble with why these values are being returned to me.

the bitmap is being loaded using BitmapFactory.decodeResource() and
the image loaded displays correctly when i draw it onto a canvas.

however, the only quirk i've encountered so far is that i cannot
retrieve the bitmap width/height.

i've even moved the loading of the images from the constructor into
the onSizeChanged() event of the view just to make sure that the
unloaded view isnt affecting the results.

any insights to this would be appreciated.


3. NullPointerException error while displaying database contents.

4. Form View Example!!

5. Device Chooser in Eclipse with ADT installed not working, no way to test with device

6. option of setWallpaper()

7. MediaRecorder.VideoSource and MediaRecorder.VideoEncoder classes can't be resolved.