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)