Hi I am developing android application in which I am trying to capture image using device camera and save it to particular location. I tried it in following manner:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("inside on create ", "inside on create ");
capturePhotoForRecordUpload();
}
public void capturePhotoForRecordUpload() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
File storageDir = new File(Environment.getExternalStorageDirectory().getPath() + "/MyApp");
if(!storageDir.exists())
storageDir.mkdirs();
if(!storageDir.exists())
storageDir.mkdir();
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory().getPath() + "/MyApp"+"/sample.jpg")));
startActivityForResult(takePictureIntent, 7);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
}
}
}
I am facing problem in above code. When I start my activity it is opening my camera. But when I click back button it again coming to on create of my activity and again opening my camera application. Is that correct behaviour or I am doing anything wrong? without calling on destroy of my activity it is again coming inside on create.Need some help. Thank you.
Its a bit unusual to launch a new activity before the launching activity is shown.
However, I suspect that things would work better if you did this later, perhaps in onResume().
You might still have a similar problem, though. You might have to keep track yourself of whether you're expecting a result, so that you don't launch the camera intent twice.