I am starting the default camera app on the android to get a picture in my app using the following code:
//create parameters for Intent with filename
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION,"Image capture by camera");
//imageUri is the current activity attribute, define and save it for later usage (also in onSaveInstanceState)
imageUri = getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
//create new Intent
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, actionCode);
and catch the picture in an onActivityResult method.
Often, this will work just fine and the device will take the picture and return it to the app, but sometimes after finishing with the camera app(by saving the image or hitting cancel) it will start the camera app a second time. How can I prevent the app from opening twice?
EDIT: Thanks to Krylez's comments I was able t开发者_JS百科o put a solution in place.
I was already using a static class to hold the image from the camera so that it could be accessed by me tabbed Activity so I also put a boolean in there. Now, before I start the Activity to handle the camera I set that boolean to true, then after checking it I set it to false so that if the onCreate method is called again it will not load the camera a second time.
Thanks to Krylez's comments I was able to put a solution in place.
I was already using a static class to hold the image from the camera so that it could be accessed by me tabbed Activity so I also put a boolean in there. Now, before I start the Activity to handle the camera I set that boolean to true, then after checking it I set it to false so that if the onCreate method is called again it will not load the camera a second time.
I was able to solve this issue by using same boolean technique but by shared preferences, storing yes or no in preferences and wraping around new intent.
String val=sharedPref.getString(...);
if(val.equals("true"))
{ launch new intent
sharedPrefEditor.putstring("..","false");
sharedPrefEditor.commit();
}
it solved the problem and camera wil not run twice. Thanks.
精彩评论