Question

I am trying to capture camera photo in may app... this is what I have: The photo is saved but on the on Activity Result, I get Null point Exception. What could I be possible missing out?

private Uri getImgUri() {
    File filePath= new File(Environment.getExternalStoragePublicDirectory   (Environment.DIRECTORY_PICTURES),APP_ALIAS);
    if(!filePath.exists()){
        if(!filePath.mkdirs())
            return null;
    }
    String timeStamp= new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String path=filePath.getPath()+File.separator+"_IMG"+timeStamp+".jpg";
    File file=new File(path);
    return Uri.fromFile(file);
}

private void startGetPicFromCam() {

    Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    Uri uri= getImgUri();
    intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);
    startActivityForResult(intent,MEDIA_CAPTURE_RESULT_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
   // super.onActivityResult(requestCode, resultCode, data);
    if(requestCode==MEDIA_CAPTURE_RESULT_CODE){
        if(resultCode==RESULT_OK){
            try{
            if(data.getData()!=null)
            Toast.makeText(this,"saved to "+data.getData(),Toast.LENGTH_LONG).show();
            else
                Toast.makeText(this,"saved to path",Toast.LENGTH_LONG).show();
            }
            catch(Exception e){
                Toast.makeText(this,e.getMessage(),Toast.LENGTH_LONG).show();
            }
        }
    }
}
Was it helpful?

Solution

Edit, it appears I didn't read your question close enough. It appears that the issue is that when you use EXTRA_OUTPUT, a null intent is passed back. If you want to get access to your data, just query the file that you passed in as an extra. See this and this question for more detailed information.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top