im trying to take a picture inside an android app, and im trying to use the android devloper tutorial:
http://developer.android.com/training/camera/photobasics.html
they bring the following code:
private void dispatchTakePictureIntent(int actionCode) {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePictureIntent, actionCode);
}
i can't understand what is this action code and what it should be for taking pictures
thanks!
Per Getting a Result from an Activity, the second parameter to startActivityForResult is used to distinguish between multiple different requests (say, if you got results from both the camera and the gallery you'd want to know where the result is from).
That same actionCode is then returned as the requestCode in onActivityResult:
protected void onActivityResult(int requestCode, int resultCode, Intent data)
The point is that it doesn't matter exactly what the number is (0, 1, 100, 5439, whatever), only that it is unique within your Activity. Therefore if you are only calling startActivityForResult in one place for one result, any number will do (as there is nothing to conflict with)
Intents are designed to allow your application to interact with others. In this case, your application will bring up the camera app, and the result will be sent back into your app.
A great place to get started with understanding this is, is the Android training "Interacting with Other Apps".
Related
I'm trying to start the camera activity from my application
startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
thats ok, after few seconds (lets say 10) I want to dispatch the touch event
and simulate the operation of taking a picture (long touch on the screen).
after that I want to catch the result back in my app...
protected void onActivityResult(int requestCode, int resultCode, Intent data)
do you have any idea how can I perform that?
thanks.
Sorry, but you cannot send a touch event to another app. Even if you could, it would not help, as there are thousands of camera apps, which use a variety of trigger mechanisms for taking a picture.
Either allow the user to take the picture, or you will need to work with the camera hardware in your own app, rather than use a third-party app.
I am looking for a light easy way, to pick an image from a gallery, and pass to another intent/activity the image selected.
This is what I have but I know it is wrong, I was just looking for some quick insight.
I know this opens up an image to be selected, but once it's selected, nothing happens, it does not load my intent. I am aware, from my belief once the image is selected it is just not dong anything and is returning true or w.e. when chosen and not providing anything, to which I ask if I need some more methods to be called that I haven't included? thank you.
Code:
item.getTitle().equals("Upload")) {
// TODO might want to pass parameter of what fragment is loaded.
// Switch to upload activity to allow for uploading of images.
Intent uploadIntent = new Intent(this, UploadActivity.class);
uploadIntent.setType("image/*");
uploadIntent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(uploadIntent, "Select Picture"),
PICK_IMAGE);
//startActivity(uploadIntent);
}
I'm not sure I fully understand your requirements but if you're ok with launching the default system image gallery, then the following post may help you:
How to pick an image from gallery (SD Card) for my app?
The general idea is to request a new Activity handle an Intent requesting a result (startActivityForResult()). If an Activity is setup to receive the intent (ACTION_GET_CONTENT) then it will launch and handle your request. Once the request is complete, the Activity will return a result which your Activity will recieve in it's method
onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent);
I'm invoking android native calculator from my app, how do i get result data from it.. means i started native calender like this, after finishing calculation i press back onActivityResult is executed and data returned is null, how to get calculated data.. Help me
Intent i = new Intent();
i.setClassName("com.android.calculator2",
"com.android.calculator2.Calculator");
startActivityForResult(i, 1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 1){
Log.i("CALCULATOR", "Result Data is"+ data);
}
}
After some testing, I'm starting to think that you can't really get something back from the calculator. Calling something with startActivityForResult doesn't mean it's going to return something other than null and since there's no way of getting out of the calculator other than pressing the back key, I think this is one of those cases.
The native calculator doesn't seem to be calling setResult(RESULT_SUCESS,intent_with_data) which is the step needed to be able to retrieve this result. Easiest thing I can think of, since you're wanting to do some calculation is to implement your own calculator class and call that one instead of the native one.
Calculators are easy to make and you have a zillion examples on the net. Just make sure you have an OK button that calls setResult(RESULT_SUCESS, intent_with_data) after you put extras to the intent with the result.
Warning
Be aware that you're hardcoding a class name instead of calling an intent by specifying an action and URI. This may call the original calculator on the emulator and standard versions of Android, but manufacturers change those kinds of things and since no one is supposed to be calling them like you intend to with your intent, you may end up crashing the app.
I am launching a media intent to take a photo. After the user took a picture and gets back to the previous activity the whole application has restarted.
As this doesn't happen all the time, I think my application goes to the background and android kills it when the device has low memory.
Is there any way to keep my application from going to the background?
Thanks!
This is normal behavior in Android, all activities currently not visible on screen (after onStop) can be killed without notice (i.e. onDestory) if the system has low memory.
This usually happens to our app on one of our test devices which has memory issues regardless of our app.
You can usually recreate this behavior when you open the camera via the intent, and then rotate the device (portrait to landscape), this will also kill and re-create your app.
To solve this you need to extend onSaveInstanceState and use the savedInstanceState parameter in your onCreate to pass from your killed instance to your new instance some important information (like "we're in the middle of getting a pic from the camera").
I can think of two possibilities...
It's not killing the activity, but the intent launches a new activity. You can stop this by putting a tag in your manifest.xml as an attribute in the activity tag like this:
<activity
android:name=".nameOfActivity"
android:launchMode="singleTop" />
Make sure that the media intent is under the activity to handle the photo, and not a main/launcher activity.
IMO two options:
Implement onSaveInstanceState/onRestoreInstanceState
or
Make your activity a service.
I faced this issue and got a solution after R&D for it.
Set Target Android 4.0 and then add this line in AndroidManifest.xml of activity:
android:configChanges="screenLayout|uiMode|orientation|screenSize|smallestScreenSize"
It works for me.
When you start media intent use following method instead of startActivity(intent)
startActivityForResult(intent, REQUEST_CODE); //private int REQUEST_CODE = 232
When the started activity finishes, your calling activity will be started. You need to handle this using following function
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
//Perform your task
}
}
The activity started need to override follwoing method
#Override
public void onBackPressed() {
Intent intent = new Intent();
setResult(REQUEST_CODE, intent);
super.onBackPressed();
}
startActivityForResult() is an special way for starting activity for a specific task and get the desired result. The called activity will send the data back.
You can use the method putExtra() & getExtra in intent to send and receive data between two activity.
This will solve your problem hopefully.
If you have doubts comment. And if possible share your code so it can be more clear.
I'd like to launch a video capture Intent that calls me back with some kind of result. So far it correctly launches the G1's default Camcorder app, but when I finish recording the video in Camcorder, it just sits there (still in the Camcorder app) instead of calling my onActivityResult() method.
Is this just a shortcoming of Camcorder, or is there a special flag or extra I can set to tell it to return control when it's done recording? Ideally it would pass me back a handle to the video - a URI, an InputStream, a file path.
The code:
protected void launchRecordVideoActivity() {
Intent intent = new Intent (android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult (intent, RequestCodes.RECORD_VIDEO);
}
RequestCodes is my own interface containing int constants.
public void onActivityResult (int requestCode, int resultCode, Intent intent) {
...
}
The contents of onActivityResult don't matter because my app is never called back at all.
onActivityResult is not being called.
Any suggestions? I can also bake the video recording directly into the app, but I'd prefer not to.
You can't (generally) call startActivityForResult on an intent that starts an application outside of your application
Same code works fine on SE x10 (updated to sdk 2.1). Activity starts, user starts recording video and when he press stop - video onActivityResult() is run in calling class.
Probably it was fixed in later SDK releases.