I'm working on a app where I have to launch the camera. In Jelly Bean the gallery and camera merged and are now the same app (com.android.gallery3d I think). I've been searching for a answer on how to launch the camera but they're all about the startActivityForResult() method. I don't want the user to return to my app and I don't want my app to get the photo.
How can this be done?
This Intent works fine for me on JellyBean:
Intent intent = new Intent(android.provider.MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
startActivity(intent);
Related
I'm working with some legacy code and the camera is opened using
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Which is fine. However, some code is running when the camera app is open (I'm not sure entirely why), but it does something if the camera app is in front of the user. The "top" app is retrieved, and then the code that checks if the camera is on top is:
boolean isCameraOnTop = topName.toLowerCase().indexOf("camera") != -1;
This was working fine for some time but we've been testing with a new device whose default camera app name is NOT "camera", but something else ("org.codeaurora.snapcam" if you must know). This approach seems flimsy as any device can have any default camera app.
SO, my question is, when I launch the camera app via the ACTION_IMAGE_CAPTURE intent, how can I find the app that actually gets opened?
After some searching, I found the solution:
activity.getPackageManager().resolveActivity(cameraIntent, PackageManager.MATCH_DEFAULT_ONLY).activityInfo.packageName;
I'm trying to start default android wallpaper chooser. I'm using:
Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER);
startActivity(intent);
This code works but it opens app chooser. I want to open "Wallpapers" directly. My minSdkVersion is set to 16.
By "default" you seem to mean the wallpaper app that came with Android OS, rather than other wallpaper apps that the device may have. You can force Android to launch a particular activity by setting the component in the intent.
Intent intent = new Intent(Intent.ACTION_SET_WALLPAPER);
intent.setComponent(...);
startActivity(intent);
However, this is a risky thing to do. If you run this code on a device that doesn't have the wallpaper app that you've specified, then you will get an ActivityNotFoundException.
Do you really need to launch one particular wallpaper app? A central feature of Android is that you say what you want to do, and it finds the app to do it. I don't know what your goal is, but another function that might be helpful is PackageManager.resolveActivity. You can use it to discover, in code, what app would be launched for a particular intent.
http://developer.android.com/reference/android/content/pm/PackageManager.html
Hope this helps.
I am using this quite simple piece of code to capture an image on an Android device.
File tmpFile = ...;
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(tmpFile));
startActivityForResult(intent, CAPTURE_IMAGE);
This works fine for thousands of users except for one running this on a Kindle device without camera. Today I got a crash report from a device with camera:
ANDROID_VERSION=4.2.1
BRAND=Hisense
PHONE_MODEL=M470BSA
STACK_TRACE=android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.media.action.IMAGE_CAPTURE (has extras) }
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1622)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
at android.app.Activity.startActivityForResult(Activity.java:3370)
at android.app.Activity.startActivityForResult(Activity.java:3331)
...
I have put some exception handling around the code to catch the exception and show an error, but I have no idea why this piece of code is failing on this device. The user confirmed that the camera is working fine.
First, there is no requirement that a device have an activity that supports ACTION_IMAGE_CAPTURE, even if the device has an actual camera.
Second, particularly on Android 4.3+ tablets, the person using the device may not have access to an ACTION_IMAGE_CAPTURE activity, even if one is installed, as the person may be running in a restricted profile.
I'm writing an app that uses the camera however I'm currently using the code:
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivity(intent);
Which opens the camera but not the stock android camera app. Is there a way to do that? I want to use the stock camera so that I don't have to deal with saving photos/videos, or exception handling myself.
I have a program that I have made for Android 1.6 and up and I have been doing tests to ensure that the program works fine with the new Ice Cream Sandwich (Android 4).
Everything on the app works fine except that when a certain task has been performed by the user it is supposed to automatically launch the android browser. However for some reason it seems to load it up in the background and keeps my app shown which is not what I want it to do.
On every other version of android when I execute the code to launch the browser the browser comes to the top of the screen therefore causing my app to be in the background which is how I wanted it to work.
Below is the code that I have to launch the browser
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse(companyURL));
startActivity(intent);
The companyURL is a variable that I am using to parse the url to the browser.
Thanks for any help you can provide.
UPDATE
I have just discovered that if the browser is not currently running (i.e. not been loaded previously) when my app starts the browser it brings it to the front. However, once the browser has been previously loaded, when my app loads it up again, it loads it in the background.
Please try this it is working on ICS for me.
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(companyURL));
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Typically you don't need the CATEGORY_BROWSABLE category. Try the following instead:
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse(companyURL));
startActivity(intent);
In fact, the documentation seems to suggest CATEGORY_BROWSABLE is intended to be used in the opposite direction (browser to your app):
Activities that can be safely invoked from a browser must support this category. For example, if the user is viewing a web page or an e-mail and clicks on a link in the text, the Intent generated execute that link will require the BROWSABLE category, so that only activities supporting this category will be considered as possible actions.
http://developer.android.com/reference/android/content/Intent.html#CATEGORY_BROWSABLE
Try adding FLAG_ACTIVITY_BROUGHT_TO_FRONT flag to the intent.
I'm using following code and its working fine with me even when browser is already running in the background.
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(intent);