密码错误的Android Adminreceiver拍照

时间:2015-04-26 13:38:19

标签: android

我这样做是为了检测pincode何时是正确的以及何时出错:

public class AdminReceiver extends DeviceAdminReceiver  {
  @Override
  public void onEnabled(Context ctxt, Intent intent) {
    ComponentName cn=new ComponentName(ctxt, AdminReceiver.class);
    DevicePolicyManager mgr=
        (DevicePolicyManager)ctxt.getSystemService(Context.DEVICE_POLICY_SERVICE);

    mgr.setPasswordQuality(cn,
                           DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC);

    onPasswordChanged(ctxt, intent);
  }

  @Override
  public void onPasswordChanged(Context ctxt, Intent intent) {
    DevicePolicyManager mgr=
        (DevicePolicyManager)ctxt.getSystemService(Context.DEVICE_POLICY_SERVICE);
    int msgId;

    if (mgr.isActivePasswordSufficient()) {
      msgId=R.string.compliant;
    }
    else {
      msgId=R.string.not_compliant;
    }

    Toast.makeText(ctxt, msgId, Toast.LENGTH_LONG).show();
  }

  @Override
  public void onPasswordFailed(Context ctxt, Intent intent) {
    Toast.makeText(ctxt, R.string.password_failed, Toast.LENGTH_LONG)
         .show();
  }

  @Override
  public void onPasswordSucceeded(Context ctxt, Intent intent) {
    Toast.makeText(ctxt, R.string.password_success, Toast.LENGTH_LONG)
         .show();
  }
}

我希望手机能够将OnPasswordFailed方法拍摄到相机胶卷中,我该怎么做?而不是简单的吐司。

1 个答案:

答案 0 :(得分:0)

您可以使用android.hardware.Camera:Camera

您需要权限:

android.permission.CAMERA

代码:

Camera camera = Camera.open();
camera.startPreview(); // this is required
// and now use the camera.takePicture() read it from documentation
camera.takePicture(null, null, new Camera.PictureCallback(){
          public void onPictureTaken(byte[] data, Camera camera){
                  OutputStream os; // os is output stream to file (Can you open files for write?)
                  try
                  {
                      os.write(data);
                      os.close();
                      // EDIT:  
                      // Check if you added permission: android.permission.WRITE_EXTERNAL_STORAGE
                      // add final modifier fist parameter (Context ctxt). Change it to: final Context ctxt 
                      // Send a broadcast to media scanner
                      // file is your saved file (java.io.File)
                      Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file));
                      ctxt.sendBroadcast(intent);
                  } catch(IOException e){
                      // EDIT: Maybe an error occurred
                      Toast.makeText(ctxt, "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                     // print it to logcat
                     e.printStackTrace();
                  } 
                 // And now this is important !!! You must call release on camera
                camera.release();
          }
});

Camera.takePicture

编辑:

请参阅此答案:https://stackoverflow.com/a/17535070/2536878