I have used this code for take photo from camera however onActivityResult() not call.
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File f = new File(android.os.Environment.getExternalStorageDirectory(),"temp.jpg");
intent.putExtra("return-data", true);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
startActivityForResult(intent, 0);
you don't have onActivityResult() method, check this related problem onActivityResult error . OP had almost working example.
There are 2 ways to take a photo :
1 - Using an Intent to make a photo
2 - Using the camera API
I think you should use the second way and there is a sample code for two of them.
Hope It helps.
Related
I am using the following code to request an image from the user:
Intent pickIntent = new Intent();
pickIntent.setType("image/* video/*");
pickIntent.setAction(Intent.ACTION_GET_CONTENT);
pickIntent.addCategory(Intent.CATEGORY_OPENABLE);
// child intents to allow capture image and video
Intent capturePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Intent captureVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
// set max capture size
capturePhotoIntent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, 20971520L); //20*1024*1024=20MB
captureVideoIntent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, 20971520L); //20*1024*1024=20MB
// present import chooser
String chooserTitle = "Import Media From...";
Intent chooserIntent = Intent.createChooser(pickIntent, chooserTitle);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{capturePhotoIntent, captureVideoIntent});
startActivityForResult(chooserIntent, MEDIA_CAPTURE);
This works on my Nexus 5, but crashes on my Galaxy S5. The crash occurs after I have taken the photo, and tap "Save," but before onActivityResult() is called. I have also tried supplying a URI via capturePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, mUri), but the same behavior occurs.
I get no stack trace for my code, since nothing is passed back to my activity; the activity just crashes.
I've been working on this for days. Any advice is appreciated.
I figured out the problem. My app was running out of memory. I solved this by adding android:largeHeap="true" to my AndroidManifest.xml, as well as redesigning the code that managed the picture data.
I have an app that a user will be able to take pictures with. Having the camera is a small, but necessary feature but I want it to only be able to take pictures (no video). Is there a way I can make it startIntentForResult with a pictures-only camera intent? Or perhaps make it only accept images as the result? Making my own custom camera for the app seems a bit overkill, but I will do it if I have to.
Thanks
This code has been working for me for ever
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Uri uri = Uri.fromFile(File.createTempFile("image", ".jpg"));
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, uri);
startActivityForResult(Intent.createChooser(cameraIntent, "Take Picture", 0);
in my app, using Gingerbread, this piece of code works:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA);
But in ICS (I tried on different ROMs), using
Uri selectedImageUri = data.getData();
returns null.
How can I make the first code work?
Thanks in advance
.
PS: I found this solution:
File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(dir, "avatar.jpeg")));
But if a device haven't a storage expansion? Plus I dont need to save the image.
I use the following code to take a picture:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(path + "/" + fileName)));
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
Now when I use this, it does save the picture where I specify above, but it also saves a second copy to the default image folder and adds it to the gallery.
I would simply delete the second file, but it seems it would be a tad dangerous as onActivityResult's intent parameter is always null after taking said picture, so I would have to attempt deleting the most recently saved picture.
Is there any way I can prevent this behavior or correct it by getting the URI of the dupicate picture?
Well I've determined that it is pretty much not possible. I am now using a SurfaceView with my own camera activity.
At the moment I am using two intents. One for voice-recording, another for the camera:
Intent photoIntent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(photoIntent, ACTIVITY_TAKE_PHOTO);
Intent voiceIntent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
startActivityForResult(voiceIntent, ACTIVITY_RECORD_SOUND);
My aim is to put an Extra to each of those which contains the path where to store the picture / the recorded Voice. Is there an option to do so?
You can use the EXTRA_OUTPUT extra to specify a destination Uri for images taken with ACTION_IMAGE_CAPTURE (but not RECORD_SOUND_ACTION; for that, the returned bundle will contain the file path).
An example can be found here, excerpt below:
Loosely quoting yanokwa:
// fire off the intent
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
Uri.fromFile(new File("<temp file path here>")));
startActivityForResult(i, mRequestCode);
BTW, a similar question can be found here.
I am not sure but my first thought would be to set the data uri of the intent and see if that does anything.
AFAIK this is not possible from firing off Intents.
When the given activity returns the picture/voice data should be in the result. Take that data and then save it from within your activity to your desired location. The camera/recorder activity simply handles pictures/audio and then returns the result back to you to handle.