0

I'm trying to build an app that when a button is pressed the camera intent is launched and the user takes a picture that is then displayed in my app. The problem I am having is that in onActivityResult the resultCode is -1 and the intent is null. I've based my code off this sample from google

My code looks like this:

private static final int CAMERA_PIC_REQUEST = 1337;
private String mCurrentPhotoPath;

public void cameraClick(View view){
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    if(cameraIntent.resolveActivity(getPackageManager()) != null){
        File photo = null;
        try{
            photo = createImageFile();
        }
        catch (IOException e){
            e.printStackTrace();
        }

        if(photo != null){
            //cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
            startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
        }
    }


}

private File createImageFile() throws IOException {
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  /* prefix */
            ".jpg",         /* suffix */
            storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
   if(requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null){
     //do image setting here
   }
}
zzz711
  • 21
  • 3
  • Do you have the camera permission in the manifest? "android.permission.CAMERA" – Ushal Naidoo Oct 15 '15 at 20:07
  • @njzk2 I'm pressing the check mark after I take a picture. – zzz711 Oct 15 '15 at 20:46
  • @RandyFreak I do have the camera permission declared. – zzz711 Oct 15 '15 at 20:49
  • my bad. -1 is indeed RESULT_OK. So, the reason why you are receiving no data is possibly because your device requires that you specify the path to the image prior to trigger the camera. see http://stackoverflow.com/questions/1910608/android-action-image-capture-intent It is likely that data is not null but data.getData is – njzk2 Oct 15 '15 at 22:28

0 Answers0