Monday, April 16, 2012

Take picture with android camera (intent) out of memory error

I'm having two troubles with the below code. It just take picture "onclick" using intent of camera android and it display the image on the ImageView.



1) After two or three pictures without leaving the activity, it crash with an outOfMemory error often when i'm rotating the display.
2) When I take picture first time, it refresh the imageview but when i do second or third time...it doesn't refresh the picture until I rotate the screen
3) I would like to save picture on internal storage instead of external, but I don't understand how to do cause I tried several tutorial and it stucks the camera!



public class HandScryActivity extends Activity {

private static int TAKE_PICTURE = 1;
private MtgMatch myMatch;
private File handFile;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.handscry);
// Disable screen saver
getWindow().addFlags(LayoutParams.FLAG_KEEP_SCREEN_ON);
// Load match
myMatch = MtgMatch.getSingletonMtgMatch();
handFile = new File(Environment.getExternalStorageDirectory(), "test.jpg");
if (myMatch.getHandUri() != null) { loadPicture(); }
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
loadPicture();
}

// Handles onGame clicked buttons
public void btnHandClick(View v) {
Button clickedButton = (Button) v;
// according to clicked button
switch (clickedButton.getId()) {
case R.id.btnBackToGame:
this.finish();
break;
case R.id.btnTakePicture:
myMatch.setHandUri(Uri.fromFile(handFile));
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, myMatch.getHandUri());
startActivityForResult(intent, TAKE_PICTURE);
break;
default:
break;
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE) {
// Display image
if (resultCode == RESULT_OK) {
loadPicture();
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
}

// Put the photo inside frame
private void loadPicture() {
ImageView img = (ImageView) findViewById(R.id.imgHand);
img.setImageURI(myMatch.getHandUri());
}

}




No comments:

Post a Comment