0

I have this code right here:

public class pantalla8 extends Activity {
    protected static final int CAMERA_REQUEST = 0;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.camera);
        final Button logout = (Button) findViewById(R.id.boton13);
        logout.setOnClickListener(new OnClickListener(){
            public void onClick(View arg0){
                finish();
            }
        });
        final Button camera = (Button) findViewById(R.id.boton12);
        camera.setOnClickListener(new OnClickListener(){
            public void onClick(View arg0){
                Intent cameraIntent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(cameraIntent, CAMERA_REQUEST);
            }
        });
    }
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAMERA_REQUEST) { 
            Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        }
    }
}

Which invokes camara's service.

With this, I can take pictures, save them and return to my app without problems. But if I don't want to save the picture, otherwise, discard it; an error occurs and Android forces the application to close.

My question is, what's missing in my code to handle that event? I mean, if the user discard a picture, the camera should be called once again.

Plus, when the user saves a picture; how do I relate those pics with my app? I think I should use a SQLite table, but I'm not sure how to do it, because, later those pictures must be sent to a server.

I would appreciate any kind of help! Thanks in advance!

Charles
  • 50,943
  • 13
  • 104
  • 142
alois.wirkes
  • 369
  • 2
  • 7
  • 20
  • could you please also post the exception stacktrace? – dongshengcn Oct 04 '12 at 14:48
  • are you using the stock camera app for testing? if yes, which device? if you want to call the camera again if no picture was taken - check for the result code - it can be (mostly) either RESULT_CANCELED or RESULT_OK – Yalla T. Oct 04 '12 at 14:50
  • For taking image with camera, see this question: http://stackoverflow.com/questions/2729267/android-camera-intent – dongshengcn Oct 04 '12 at 14:50
  • What do you mean by "relate those pics with my app"? – dongshengcn Oct 04 '12 at 14:51
  • -1 for not posting the stacktrace. however, the issue come from you not testing the resultCode and not testing if getExtras() contains something at all. in case the result is CANCELED, there won't be anything there. – njzk2 Oct 04 '12 at 14:56
  • why people are in a hurry to down vote? Please give the questioner some time. – karn Oct 04 '12 at 18:17
  • I am upvoting the question not cause of the quality of the question but to negate the unnecessary down vote. – karn Oct 04 '12 at 18:19

1 Answers1

0

The only problem is that you are not handling the case when user discards the captured image. When user discards the captured image then onActivityResult() is called with resultCode = RESULT_CANCELED. Here is a sample for handling the onActivityResult() for camera.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    /*
     * setting images from camera click intent
     */
    try {
        if (requestCode == CAMERA_REQUEST) {
            if (resultCode == RESULT_OK) {
                 //here you can save the image or do something

            } else if (resultCode == RESULT_CANCELED) {
                // User cancelled the image capture
            } else {
                // Image capture failed, advise user
            }
        }
    } catch(Exception e) {
        Toast.makeText(yourActivity.this, "Unable to load image !!",  Toast.LENGTH_SHORT).show();
    }
}

I hope this will help. Feel free to discuss.

Bista
  • 7,869
  • 3
  • 27
  • 55
karn
  • 5,963
  • 3
  • 22
  • 29