简体   繁体   中英

problems taking photo “onActivityResult”

I have a problem with the camera and in many cases to take the picture and take it to the ImageView, get error.

java.lang.RuntimeException: Unable to resume activity {co.com.xxxx.xxxx/xxxx.xxxx.screens.formularioScreen}:
 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent
 { act=inline-data (has extras) }} to activity {xxxx.xxxx,screens/xxxx.xxxx.screens.formularioScreen}:
 java.lang.NullPointerException

My code to take photo is the following:

    Button oButton = new Button(this);
    oButton.setText("take Photo");
    oButton.setOnClickListener(new OnClickListener() {              
        public void onClick(View v) {

            oImageActual = oView; //variable final = ImageView
            Intent intent =  new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent, TAKE_PICTURE);
        }                       
    });

//layout = container
layout.addView(oButton);

and I have the method onActivityResult

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == TAKE_PICTURE) {          
        if (data != null) {             
            if (data.hasExtra("data")) {
                oImageActual.setImageBitmap((Bitmap) data.getParcelableExtra("data"));
            }               
        }
    }
}

Not if I'm missing some try / catch or something, but the error happens frequently.

Any ideas?


The error still appears, there are times when it does not, and others so.
Try placing it oneilse14 said,

oImageActual.setImageBitmap((Bitmap) data.getExtras().get("data"));

but the error remained the same. Anyone know why this happens or how to prevent it?


Start the camera with a button

oButton.setText("Take photo");
oButton.setOnClickListener(new OnClickListener() {              
    public void onClick(View v) {       
        oImageActual = oView; //oImageActual ImageView is a final variable = new ImageVew(this);
        Intent intent =  new Intent(MediaStore.ACTION_IMAGE_CAPTURE);                           
        startActivityForResult(intent, TAKE_PICTURE);
    }                       
});
LinAyout.addView(oButton);

And this is the onActivityResult.

/**
 * Al terminar la actividad de la camara, se ejecuta este metodo para continuar
 */
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);      
    if (requestCode == TAKE_PICTURE && (resultCode == Activity.RESULT_OK)) {            
        if (data != null) {         
            if (data.hasExtra("data")) {                
                oImageActual.setImageBitmap((Bitmap) data.getParcelableExtra("data"));
            }   
        }
    }
}

After calling startActivityForResult(intent, TAKE_PICTURE); your Activity goes to background. When your device need more resources for CameraActivity your inactive Activity can be killed. So if you had some initialized variables inside your activity before it goes to background, you should be ready that they won't be initialized after your activity comes back to foreground.

不要调用getParcelableExtra(),而是尝试使用

oImageActual.setImageBitmap((Bitmap) data.getExtras().get("data"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM