I'm trying to use the camera to take pictures in my app, and then crop the taken images.
Everything is working for the recent versions for Android, but not for Android Kitkat 4.4.2.
the Camera returns a null URI.
get the URI onActivityReslult :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_CODE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
picUri = data.getData();
Intent i = new Intent(PublierActivity.this, CropActivity.class);
i.putExtra("Uri", picUri);
startActivityForResult(i, CROP_CODE);
}
Here is how I call the camera intent :
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, CAMERA_CODE);
}
is there any way to make an exception for oldest versions for android to resolve this problem ?
From your code, it would appear that you are trying to use ACTION_IMAGE_CAPTURE. In that case, there should never be a Uri — data.getData() should always be returning null. If a camera app happens to return a Uri, that may be the image, but since ACTION_IMAGE_CAPTURE is not documented to return a Uri, you have no way of knowing what that Uri is for.
If you are using EXTRA_OUTPUT on the ACTION_IMAGE_CAPTURE Intent, you know where the image should be stored, because you told the camera app where to store it. Note that some camera apps are buggy and fail to honor EXTRA_OUTPUT, putting the image wherever they want.
If you are not using EXTRA_OUTPUT, then you will get a thumbnail back in the "data" extra.
Also, please bear in mind that this has nothing to do with Android OS version, and everything to do with the camera app that the user chooses to use. There are thousands of Android device models. These ship with dozens, if not hundreds, of different camera apps pre-installed. The user might also elect to install a third-party camera app. Any of those could be handling your request.
Related
Let's say I have a method inside my activity that looks like this:
private void dispatchTakeVideoIntent() {
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (takeVideoIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
}
}
Then if everything works out fine, I access the taken video by its URI:
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent intent) {
if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == Activity.RESULT_OK) {
Uri videoUri = intent.getData();
// process video
}
}
Where does the video actually reside at this point? Is it temporarily stored in memory or does it get saved to permanent storage?
If I want my app to reliably access that video at some later point, what's the best way to go about this? Do I have to copy the video to my app's storage folder(s)?
Where does the video actually reside at this point?
It will reside wherever the user's camera app elects to put it. There are hundreds of different camera apps, both pre-installed by manufacturers and user-installed from the Play Store or elsewhere.
Is it temporarily stored in memory or does it get saved to permanent storage?
That is up to the camera app.
If I want my app to reliably access that video at some later point, what's the best way to go about this?
You can try passing a content Uri value via EXTRA_OUTPUT as an extra on your Intent. You can use FileProvider to get that Uri, pointing to some file in a location that you control (e.g., getFilesDir() on Context). Most camera apps should honor this extra, but not all will.
Or, you could copy the video, as you suggest.
Or, you could use a camera library (e.g., FotoApparat, CameraKit-Android) and record the video within your own app.
So I have a Fragment that calls the following method which launches the camera:
private void launchCamera() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
And I expect to receive the picture data in this method in my fragment:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (!(resultCode == RESULT_OK)) return;
if (requestCode == REQUEST_IMAGE_CAPTURE || requestCode == REQUEST_GALLERY_IMAGE) {
Uri imageURI = data.getData();
// do something
}
}
However, after I take a picture and confirm it, the app goes to my launcher. After setting breakpoints in the onActivityResult method, the app never even reaches this method before crashing. I've granted made sure to grant all permissions in both the manifest and at runtime.
There are also no outputs to Logcat with this crash, both in the app logs and the device logs. I have also tested on both my device (Moto G5 Plus) & Pixel XL API 26 emulator; both have the same result.
I think you need to call super.onActivityResult().
The fragment is the one making the startActivityForResult() call, but the activity gets the first shot at handling the result so you need to implement super.onActivityResult() to make the fragment handle the result.
the app immediately crashes to my launcher
No, the camera app immediately brings up the launcher. Apparently, the developers of this camera app wrote it to bring up the home screen when the user is done taking the picture. This is a bug, of course, but there is little that you can do about it.
Use ACTION_IMAGE_CAPTURE when you would like a picture but do not mind if you do not get it, due to bugs in some of the hundreds of camera apps that you are integrating with. Otherwise, use a camera library (Fotoapparat, CameraKit-Android, etc.) to take the picture directly in your own app.
Also, and FWIW, ACTION_IMAGE_CAPTURE does not return a Uri, so your onActivityResult() code will not work anyway. Given your particular ACTION_IMAGE_CAPTURE Intent configuration, in onActivityResult(), data.getParcelableExtra("data") will return a Bitmap representing a thumbnail-sized image.
I believe this is yet another manifestation of the Android: Activity getting Destroyed after calling Camera Intent.
The bottom line is, the system restarts your app from scratch and your activity is created but has a chance to restore its state.
So you must implement onRestoreInstanceState(). I am not sure you can guarantee that the fragment will be ready to receive onActiviyResult() timely, so to be in the safe side, I prefer to handle the captured image in activity itself.
So this issue was actually because of an intent flag I had attached to the activity. My activity was started using the NO_HISTORY intent flag, which apparently prevented it from being recreated when returned from a startActivityForResult call.
In Android, taking a picture can be done with this basic setup as seen here.
protected void dispatchTakePictureIntent(){
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
...
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
//do stuff with the output file
}
}
However, third-party camera apps are unpredictable and cannot be guaranteed to play nice.
What is the correct way to validate the output / catch any errors? Should I be wrapping startActivityForResult inside of a try/catch block? What are the various types of exceptions I should be trying to catch and what should I do with them?
Should I be wrapping startActivityForResult inside of a try/catch block?
Yes, though this has nothing to do with ACTION_IMAGE_CAPTURE specifically. The user may not have access to a camera app (e.g., the user is running under a restricted profile on a shared device). Any time you call startActivity() or startActivityForResult() with an implicit Intent, you need to deal with the possibility that there is no available app.
What are the various types of exceptions I should be trying to catch and what should I do with them?
In the answer that you linked to, I was pointing out crashes in the camera app. You cannot catch those, as that is not your app.
Beyond ActivityNotFoundException, you also need to think about Android 6.0 runtime permissions, as you need the CAMERA permission to start an ACTION_IMAGE_CAPTURE activity. Usually, though, this will not involve an exception handler.
The other validations would be not based on exceptions, but based on flawed results:
If you supplied EXTRA_OUTPUT, is there any content at the Uri you specified? In theory, if the user did not cancel out of the camera app, there will be, but that is not guaranteed, due to camera app bugs.
If there is is content, is it plausibly an image file? For example, you might use BitmapFactory with inJustDecodeBounds to see if the image really is an image.
If there is no content, did you happen to get a Uri back in the result Intent in onActivityResult()? The buggy camera app might have stored the image where it wanted to and supplied the Uri to it, even though this is way out of spec for ACTION_IMAGE_CAPTURE.
If there is no content, and there is no Uri, does getExtras("data") return a non-null value? In that case, you at least got a thumbnail and can save it yourself, even though the image quality may suck.
I would like to allow the user to take a photo, and when finished, have it passed directly into a variable (Bitmap preferably) instead of saved to memory. I've tried using the default android camera intent but cannot seem to stop it from saving to memory and showing up in the gallery.
Any suggestions?
What I have so far is this:
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, 1);
}
But this saves the picture in the gallery. I would like to get the image without saving it in the gallery.
Here's a nice example given at official android developer site: http://developer.android.com/training/camera/photobasics.html
Hope this would be helpful.
I want to have a way to trigger the camera on Google Glass right after I get in (e.g., "Ok Glass", "App", ) but I cannot find any similar examples online.
Any idea how to trigger this?
Also, currently when I try to create a new Android project it will be the default Hello World, is there a template for Google Glass?
On Ok glass you can write like -
String fileUri ="file:///mnt/sdcard/Pictures/" ;
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // file:///mnt/sdcard/Pictures/MyCameraApp/IMG_20130812_105617.jpg
startActivityForResult(intent, TAKE_PICTURE_REQUEST);
Then you have to call -
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == TAKE_PICTURE_REQUEST && resultCode == RESULT_OK) {
String picturePath = data.getStringExtra(
CameraManager.EXTRA_PICTURE_FILE_PATH);
}
super.onActivityResult(requestCode, resultCode, data);
}
It will work.
If you want to launch the built-in camera application immediately at the beginning of your application, you can call startActivityForResult with an intent whose action is MediaStore.ACTION_IMAGE_CAPTURE inside your initial activity's onCreate method. (But note the differences between Android and Glass with respect to how the captured photo is returned; see the Glass Camera class Javadoc for more details.)
Eclipse and Android Studio do not currently have any special support to generate Glass projects out of the box.
You can use my sample Camera Snapshot on GitHub. It does exactly what you're asking.
Here it is:
https://github.com/dazza222/GlassCameraSnapshot