ANDROID.UF.CAMERA

当尝试使用已被释放的资源时,就会报告 UF(使用已释放)问题。ANDROID.UF.CAMERA 警告表明存在以下尝试:在照相机释放后,使用该照相机。

示例 1

复制
     private Camera camera;
 
     private void initCamera() {
         camera = Camera.open();
     }
 
     public boolean onKeyDown(final int keyCode, final KeyEvent event) {
         if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER) {
             if (camera != null) {
                 final CameraPictureCallbackImpl callback = new CameraPictureCallbackImpl();
                 camera.takePicture(null, null, callback);
             }
             return true;
         }
         return super.onKeyDown(keyCode, event);
     }
 
     private class CameraPictureCallbackImpl implements Camera.PictureCallback {
         private Bitmap bitmap;
 
         public void onPictureTaken(final byte[] bytes, final Camera camera) {
             camera.stopPreview();
             camera.release();
             bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
             if (bitmap == null) {
                 camera.startPreview();
             }
         }
 
         public Bitmap getBitmap() {
             return bitmap;
         }
     }

针对第 46 行的代码段报告 ANDROID.UF.CAMERA,因为即使相机已在第 43 行释放,onPictureTaken(...) 回调方法也会启动相机预览。

扩展

此检查器可通过 Klocwork 知识库进行扩展。有关详情,请参阅调整 Java 分析。