0

i did this to detect when the pincode is the right and when it's wrong :

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();
  }
}

And i want the phone to take a picture into the OnPasswordFailed method into the camera roll how can i do that ? instead of the simple toast.

ouya
  • 213
  • 1
  • 3
  • 10

1 Answers1

0

You can use android.hardware.Camera: Camera

You need a permissions:

android.permission.CAMERA

Code:

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

Edit:

See this answer: https://stackoverflow.com/a/17535070/2536878

Community
  • 1
  • 1
krystian71115
  • 1,927
  • 17
  • 36
  • I added the code to the Onpasswordfailed method but it doesnt add me any pictures to the camera roll . – ouya Apr 26 '15 at 17:12
  • Maybe the media scanner not added picture to the database. Try to find the file using file explorer. I'm editing the answer now (write the simple code to send the broadcast to media scanner for scan the file) wait for edit. – krystian71115 Apr 26 '15 at 17:26