Reputation:
I am trying to take two picture with my App first picture takes without problem but taking another picture give me NullPointerException.
Following code is my Camera activity. Last two days i am trying to solve this issue but so far have no luck.
Edit: Where exactly camera fails? When user take picture i show them a preview in another activity. If user wants to take another picture and click Take Another button. Activity returns to TakePicture class. This is the main problem when user takes more than one picture.
Also displayGuide() method pop up twice when second shot
appreciate for any help.
public class TakePicture extends Activity
{
private SurfaceView mySurfaceView;
private SurfaceHolder holder;
private Camera myCamera;
private int orient;
@Override
public void onCreate(Bundle b)
{
super.onCreate(b);
b = getIntent().getExtras();
orient = b.getInt("orient");
// Switch to Screen orientation based on BARCODE
switch (orient)
{
case 3 :
case 4 :
case 5 :
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
break;
default :
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
break;
}
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_fullscreen);
myCamera = getCameraInstance();
mySurfaceView = (SurfaceView) findViewById(R.id.surface_view);
holder = mySurfaceView.getHolder();
holder.addCallback(mSurfaceListener);
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
@Override
protected void onResume()
{
if (myCamera == null)
myCamera = getCameraInstance();
super.onResume();
}
@Override
public void onPause()
{
super.onPause();
releaseCamera();
}
private void releaseCamera()
{
if (myCamera != null)
{
myCamera.setPreviewCallback(null);
myCamera.release();
myCamera = null;
}
}
private static Camera getCameraInstance()
{
Camera c = null;
try
{
c = Camera.open();
}
catch(Exception e)
{
new ErrLog(e.toString());
}
return c;
}
private final ShutterCallback shutterCallback = new ShutterCallback()
{
public void onShutter()
{
AudioManager mgr = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mgr.playSoundEffect(AudioManager.FLAG_PLAY_SOUND);
}
};
private SurfaceHolder.Callback mSurfaceListener = new SurfaceHolder.Callback()
{
public void surfaceCreated(SurfaceHolder holder)
{
//myCamera = Camera.open();
try
{
myCamera.setPreviewDisplay(holder);
}
catch (Exception e)
{
new ErrLog(e.toString());
}
}
public void surfaceDestroyed(SurfaceHolder holder)
{
//myCamera.release();
//myCamera = null;
}
@SuppressLint("InlinedApi")
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
{
myCamera.stopPreview();
Parameters mParam = myCamera.getParameters();
List<Size> getPictureSize = mParam.getSupportedPictureSizes();
Size getPicSize = getPictureSize.get(5);
mParam.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
mParam.setPictureSize(getPicSize.width, getPicSize.height);
mParam.setRotation(fixPictureOrientation());
myCamera.setParameters(mParam);
Display display = getWindowManager().getDefaultDisplay();
if (display.getRotation() == Surface.ROTATION_0)
myCamera.setDisplayOrientation(90);
else if (display.getRotation() == Surface.ROTATION_270)
myCamera.setDisplayOrientation(180);
// Display guide
displayGuide();
}
};
private int fixPictureOrientation()
{
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(Camera.CameraInfo.CAMERA_FACING_BACK, info);
int rotation = getWindowManager().getDefaultDisplay().getRotation();
int degrees = 0;
switch (rotation)
{
case Surface.ROTATION_0 :
degrees = 0;
break; // Natural orientation
case Surface.ROTATION_90 :
degrees = 90;
break; // Landscape left
case Surface.ROTATION_180 :
degrees = 180;
break;// Upside down
case Surface.ROTATION_270 :
degrees = 270;
break;// Landscape right
}
int rotate = (info.orientation - degrees + 360) % 360;
return rotate;
}
private PictureCallback mPictureListener = new PictureCallback()
{
@Override
public void onPictureTaken(byte[] data, Camera camera)
{
String fileName = "sample_" + (System.currentTimeMillis() / 1000) + ".jpg";
try
{
File file = new File(PATH + "/" + fileName);
FileOutputStream out = new FileOutputStream(file);
out.write(data);
if (out != null)
out.close();
}
catch (Exception e)
{
new ErrLog(e.toString());
}
// Refreshing SD card
new UpdateSDCard().mediaScan(TakePicture.this, PATH);
// Stop Preview
myCamera.stopPreview();
Intent i = new Intent(TakePicture.this, PreviewPictureActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.putExtra("orient", orient);
startActivity(i);
}
};
public void takePicture(View v)
{
if(myCamera != null)
myCamera.takePicture(shutterCallback, null, mPictureListener);
}
public void cancelActivity(View v)
{
Intent i = new Intent(TakePicture.this, MenuActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
TakePicture.this.finish();
}
private void displayGuide()
{
GetScreenSize size = new GetScreenSize();
final Dialog dialog = new Dialog(TakePicture.this);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.guide_layout_picture);
dialog.setCanceledOnTouchOutside(false);
dialog.setCancelable(false);
ImageView close = (ImageView) dialog.findViewById(R.id.imageView1);
close.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
dialog.dismiss();
if(myCamera == null)
{
myCamera = Camera.open();
}
myCamera.startPreview();
}
});
dialog.show();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = dialog.getWindow();
lp.copyFrom(window.getAttributes());
// This makes the dialog take up the full width
lp.width = (int) (size.getScreenW() * 0.8F);
lp.height = (int) (size.getScreenH() * 0.7F);
window.setAttributes(lp);
}
}
Here is Output log
11-07 11:14:28.998: E/AndroidRuntime(1502): FATAL EXCEPTION: main 11-07 11:14:28.998: E/AndroidRuntime(1502): java.lang.RuntimeException: Fail to connect to camera service 11-07 11:14:28.998: E/AndroidRuntime(1502): at android.hardware.Camera.native_setup(Native Method) 11-07 11:14:28.998: E/AndroidRuntime(1502): at android.hardware.Camera.(Camera.java:371) 11-07 11:14:28.998: E/AndroidRuntime(1502): at android.hardware.Camera.open(Camera.java:344) 11-07 11:14:28.998: E/AndroidRuntime(1502): at main_app.TakePicture$4.onClick(TakePicture.java:236) 11-07 11:14:28.998: E/AndroidRuntime(1502): at android.view.View.performClick(View.java:4203) 11-07 11:14:28.998: E/AndroidRuntime(1502): at android.view.View$PerformClick.run(View.java:17189) 11-07 11:14:28.998: E/AndroidRuntime(1502): at android.os.Handler.handleCallback(Handler.java:615) 11-07 11:14:28.998: E/AndroidRuntime(1502): at android.os.Handler.dispatchMessage(Handler.java:92) 11-07 11:14:28.998: E/AndroidRuntime(1502): at android.os.Looper.loop(Looper.java:137) 11-07 11:14:28.998: E/AndroidRuntime(1502): at android.app.ActivityThread.main(ActivityThread.java:4961) 11-07 11:14:28.998: E/AndroidRuntime(1502): at java.lang.reflect.Method.invokeNative(Native Method) 11-07 11:14:28.998: E/AndroidRuntime(1502): at java.lang.reflect.Method.invoke(Method.java:511) 11-07 11:14:28.998: E/AndroidRuntime(1502): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004) 11-07 11:14:28.998: E/AndroidRuntime(1502): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771) 11-07 11:14:28.998: E/AndroidRuntime(1502): at dalvik.system.NativeStart.main(Native Method)
Upvotes: 2
Views: 1812
Reputation: 887
You can use errorcallback method.Camera recreate.
ErrorCallback CEC = new ErrorCallback()
{
public void onError(int error, Camera camera)
{
Log.d("CameraDemo", "camera error detected");
if(error == Camera.CAMERA_ERROR_SERVER_DIED)
{
Log.d("CameraDemo", "attempting to reinstantiate new camera");
camera.stopPreview();
camera.setPreviewCallback(null);
camera.release(); //written in documentation...
camera = null;
camera = Camera.open();
}
}
};
Upvotes: 0
Reputation:
Okay guys. I think i have found problem. Problem was myCamera = getCameraInstance(); line in onCreate method, so i removed that line and camera is not failing anymore. Hope this help someone else.
Upvotes: 1
Reputation: 997
I don't see exactly where on your code it's crashing and the logs don't give the exact line.
Apparently it's crashing when you get back on this activity so you should try to do it step by step in debug mode to see which line is the problem.
Add a breakpoint at your Activity.onPause() to check if you pass in and if the camera is well released when you go showing the preview of the pic. Then check at the beginning of your Activity.onCreate() and if it goes through (normally not you should go directly hit the onResume()) and do the same in the onResume().
Normally you should get some more hints.
Upvotes: 0