Android default camera app opens twice - android

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 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.

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.

Related

It is necessary to create an camera to put it inside an fragment (android)?

I just need to put a camera inside one of the three fragments of my main activity, like ([f]-[f]-[C]), where () is my main activity, [] is a fragment, C is the camera and f are just a fragment (they are full screen swipable). I need to create a whole camera (coding , etc) just for it or it is possible to call android native camera app with intent to an fagment?
I need to create a whole camera (coding , etc) just for it
Yes, whether you write it yourself or use one from a library.
or it is possible to call android native camera app with intent to an fagment?
No, you cannot embed a third-party app in a fragment of your app.
If you need to take a picture, you can just use an intent to launch the system camera app. Doing so will make it a lot easier to code, but you won't be able to show a live preview, as you're actually handling control to the camera app through that intent.
Manually handling the entire camera lifecycle allows you to have control over the preview and show it real-time in your app. Also, if you need to have the live preview in your app, this is the way to go and can't be accomplished using just an Intent.
You might find the UltimateAndroidCameraGuide on GitHub very helpful for your problem, particularly the SimpleCameraIntentFragment and the NativeCameraFragment files in that repo.
You can use an Intent to launch the camara and it will launch the camara default app., just be careful to detect when your "C" fragment is displayed, here: How to determine when Fragment becomes visible in ViewPager
If you dont do that, android pre-caches fragment before showing it and your intent will fire.
On your activity use:
#Override public void onResume() {
super.onResume();
if(viewPager.getCurrentItem() == 2){
//Your code here. Executed when fragment is seen by user.
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
}
See for camera launch with intent options: http://developer.android.com/guide/topics/media/camera.html#intent-image

Activity gets killed after returned from the camera

In my app I call the system camera to take a picture, and then handle the result in onActivityResult. It used to work, but now my calling activity gets killed sometimes,
sometimes it works well.I need the big picture,so I must use intent.putExtra(MediaStore.EXTRA_OUTPUT, output),if without this(like use the intent data to get a bitmap) ,it works fine.
after take a pic, and onclick the 'OK' button, I need it return to the activity
which start the camera,but sometime it works fine,sometimes the parent activity is finished. after search,I set android:alwaysRetainTaskState="true",but this not work.
my system is Galaxy S I9000,I also test this on other phone,but it works well.
did anybody know why?
here is my code
private void startTakePhoto(){
App.fileNameWithPath = Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator + System.currentTimeMillis()+".jpg";
File file = new File(App.fileNameWithPath);
Uri output = Uri.fromFile(file);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, output);
boolean flag = BaseUtil.hasImageCaptureBug();
System.out.println(flag);
//intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(fileNameWithPath)));
//BaseUtil.saveImagePath(App.fileNameWithPath, this);
startActivityForResult(intent, REQUEST_TAKE_PHOTO);
}
It like this :
I want is
1. A start B (stack: A,B)
2. B start camera activity and wait For Result (stack:A,B,camera)
3. save picture,return B activity (stack: A,B)
but on step 3, it not return to B,but A.
It seems like B is finished by the system, why?
http://developer.android.com/reference/android/app/Activity.html
The reason is because memory menagement in Android. As i know your phone has about 100mb free operative memory, if your activity is not on foreground it can be destroyed. So that's why you should implement some methods of you activity to start it is onPause, onDestroy and on Resume methods. Just save all your info in Bundle and start Activity propelly.

Camera or Gallery intent destroys old activity on some devices

I am working on app which uses WebView to display its content. However, it needs to open camera or gallery in order to choose picture:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 1);
Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(galleryIntent, 2);
It's working fine on most devices, but on HTC One and few others both intents destroys my activity, so webview is being reloaded while going back. I don't have noHistory flag in AndroidManifest.xml. What might be causing that issue? Can I avoid destroying my activity here?
It is normally, that Android kills your Activity when other app runs.
You must save Activity state in onSaveInstanceState and when activity will be recreated restore state in onRestoreInstanceState or in onCreate.
To restore state of WebView you may use cookies and sessions and save last opened url. When activity will be recreated just navigate WebView last saved url and process result from camera.
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 1);
By seeing your code I can judge that your motto is to capture the image and use it later on.
This is a known bug, The solution is that you need to create a separate folder for your application and before capturing you need to be sure that file is created and the same path you are giving to camera intent
Uri uriSavedImage=Uri.fromFile(new File("/sdcard/seperate/newImage.png"));
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra("output", uriSavedImage);
startActivityForResult(cameraIntent, 1);
Reference: image from camera intent issue in android
Maybe a stupid sugestion.
But since it's destroyed, it means the device was low on memory.
If the only annoyance is that the webview reloads, maybe you can solve this by caching the content?
For example in the onStop() method of you activity get the content of the webview and store it somewhere. temporary file, sqlite,... . and in onCreate check if there is a cache (and maybe how old it is) and if needed put that in the webview.
Tutorial to get html code from webview: http://lexandera.com/2009/01/extracting-html-from-a-webview/
If i m not wrong you are opening camera from device.Have you check that other app is not aquired the camera? you must acquire camera before starting camera activity may be some other app using camera instance.You must release the camera instance in on destroy or onstop method of activity so that next time it will available fr other app to use it or for your app to use.

Open camera app with a thunbnail icon

I want to launch Android built-in camera app with a thumbnail icon on the right-bottom. Here is my code to open the camera app.
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
I think I need to put a intent data to do it. I look forward to your help.
Thanks.
Just create an image button, or button or anything you desire really.
In the onclick event add something like:
String saveFileName= Environment.getExternalStorageDirectory() + "/test.png";
// BUILT IN CAMERA
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
camera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(saveFileName) );
this.startActivityForResult(camera, 1);
Make sure you have necessary permissions set. Like saving to the SDCard.
You are very close, you dont need those flags set. And you should ideally specify where you are saving the image.
If you are wondering how to make the button take a look here

Android - How to start recording video automatically when calling the camera intent

The Android Dev has some easy code describing how to start the camcorder via Intents.
Now this is good if you just want to start up the camera and wait for the user to press the red "REC" button.
But I want to call the camcorder via Intent and tell it to start recording programmatically.
How to I do that?
Do I pass some kind of start() method in the Intent command?
(if it can't be done, please show me a simple code bit that can be set up to record video automatically - I have been searching the web, but all codesnippets regarding this issue don't work)
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
private Uri fileUri;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
For this you should use MediaRecorder class.
Please have a look at this :
http://developer.android.com/reference/android/media/MediaRecorder.html
I've found a workaround on a rooted device. First, start the recording the usual way with the Intent (using startActivity(), not startActivityForResult()). Second, send the CAMERA key code with 'input keyevent 27'. Its magic! It starts the recording. You probably should press back (code 4) after the end of the recording.
The whole keys sequence is:
CAMERA: starts the recording (the timer appears on screen). To send a bit later after sending the
intent for safety,
DPAD_DOWN, DPAD_RIGHT and finally DPAD_CENTER are
needed to validate the shootage!
BACK to return to your activity.

Categories

Resources