@@ -41,7 +41,6 @@ public class CameraFragment extends Fragment implements TextureView.SurfaceTextu
4141 private int mCameraId ;
4242
4343
44-
4544 /**
4645 * Determine the current display orientation and rotate the mCamera preview
4746 * accordingly.
@@ -133,13 +132,14 @@ public void onResume() {
133132 */
134133 public void takePicture () {
135134 orientationListener .rememberOrientation ();
136-
137- mCamera .takePicture (null , null , this );
135+ if (mCamera != null ) {
136+ mCamera .takePicture (null , null , this );
137+ } else {
138+ Toast .makeText (getActivity (), "Unable to take a picture because the camera is not connected. :(" , Toast .LENGTH_LONG ).show ();
139+ }
138140 }
139141
140142
141-
142-
143143 private Bitmap overlay (Bitmap bmp1 , Bitmap bmp2 , float left , float top , int parentWidth , int
144144 parentHeight ) {
145145 //Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1
@@ -175,10 +175,10 @@ private Bitmap bitmapScaler(Bitmap bitmap, int newWidth, int newHeight) {
175175
176176 Canvas canvas = new Canvas (scaledBitmap );
177177 canvas .setMatrix (scaleMatrix );
178- Paint paint =new Paint (Paint .FILTER_BITMAP_FLAG );
178+ Paint paint = new Paint (Paint .FILTER_BITMAP_FLAG );
179179 paint .setAntiAlias (true );
180180 canvas .drawBitmap (bitmap , middleX - bitmap .getWidth () / 2 , middleY - bitmap .getHeight () /
181- 2 ,paint );
181+ 2 , paint );
182182
183183 return scaledBitmap ;
184184 }
@@ -306,19 +306,21 @@ public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int hei
306306
307307 @ Override
308308 public void onSurfaceTextureSizeChanged (SurfaceTexture surface , int width , int height ) {
309- // Ignored, Camera does all the work for us
309+ // Ignored, Camera does all the work for us
310310 }
311311
312312 @ Override
313313 public boolean onSurfaceTextureDestroyed (SurfaceTexture surface ) {
314- mCamera .stopPreview ();
315- mCamera .release ();
314+ if (mCamera != null ) {
315+ mCamera .stopPreview ();
316+ mCamera .release ();
317+ }
316318 return true ;
317319 }
318320
319321 @ Override
320322 public void onSurfaceTextureUpdated (SurfaceTexture surface ) {
321- // Invoked every time there's a new Camera preview frame
323+ // Invoked every time there's a new Camera preview frame
322324 }
323325
324326
0 commit comments