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;
Related
I recently updated my Poco X3 NFC 64GB test device to Android 11:
MIUI Global 12.0.8(RJGEUXM)
Android 11 RKQ1.200826.002
Ever since I am unable to take or pick photos in my app. On other Android 11 devices (and emulator) everything works fine. The app is compiled to API level 30.
I start the photo picker chooser like this:
private void pickPhoto() {
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
if (intent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(intent, 100);
}
}
This works fine, but after I have picked a photo it returns to the app, triggering onActivityResult with resultCode 0 (RESULT_CANCELED) and data is null too. On other Android 11 devices I can retrieve the photo from the data, but not on my Poco. Yes I did add the necessary query to the manifest file (else I wouldn't be able to open the picker in the first place).
When I try to fetch a photo from the camera I have exactly the same issue. Again on other Android 11 devices this works fine. So yes I did add and use the necessary file provider.
Sometimes a toast pops up about not having granted the right permissions. Do any of you guys have a Poco phone and like to try it out? I wonder if the Poco rom is just broken or something.
UPDATE
I've narrowed down the problem. It turns out that if startActivity() starts a camera/gallery activity directly it works fine. However if you're prompted a chooser first (e.g. because you have multiple gallery apps) it goes wrong.
So if you pick a gallery app and say to always use it in the future, the next time it will open that gallery app directly and then it works correctly.
If you use Intent.createChooser() it never works right. Even if it contains only one intent that is launched directly without prompting a chooser it fails to return a photo.
Can we agree that this is a MIUI bug?
I am using MediaStore.Audio.Media.RECORD_SOUND_ACTION to open sound recorder application, I am not able to open application as no default application present, then i install two voice recorder application even though not able to see these application in my chooser intent. I am using following code-
Intent soundRecorderIntent = new Intent(); // create intent
soundRecorderIntent.setAction(MediaStore.Audio.Media.RECORD_SOUND_ACTION); // set action
startActivityForResult(soundRecorderIntent, ACTIVITY_RECORD_SOUND); // start activity
It works well in marshmallow
The code is correct and you've probably found an app that supports the intent by now. But to prevent that others waste their time like I did, here's a quick note:
Most of the currently top rated voice recording apps in the Play Store do not provide the necessary intent filter.
That seemed so unrealistic to me that I doubted my code when it didn't work after having installed the five most popular voice recording apps. But there's a handy manifest viewer app that reveals that their manifests simply do not declare the intent filter. So, as said, the code is correct.
To save you the time from searching for a suitable app, here are two that are invocable:
Audio Recorder from Sony
Samsung Voice Recorder from Samsung
There is no requirement for a voice recorder app to support this Intent action, and apparently your device does not ship with an app that supports this Intent action either. Your code itself seems fine.
If recording is optional to your app, catch the ActivityNotFoundException and tell the user that they do not have a recorder, and perhaps suggest to them that they install one that you test that works, if you can find one.
Otherwise, record the audio yourself, using MediaRecorder.
I am using camera intent to take a picture in a Service by using following code
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
getApplication().startActivity(intent);
} catch (final ActivityNotFoundException e) {
e.printStackTrace();
} catch (final Exception e) {
e.printStackTrace();
}
It can take the picture; however, every time it shows the preview GUI (having captured picture, Save and Discard buttons). I want to ignore the step, just likes the default capture camera (capture and saving without preview). How could I modify the code? Thank all
Note that: I do not want to use camera2API to make a new app. I want to use the default camera app. Someone said that
"The camera app doesn't give other apps the option to disable the confirmation screen, even though the camera app itself doesn't show the confirmation screen.
Therefore it is not possible for not showing the confirmation screen."
It looks bad new in the past. I am using Android 5.0. Is it possible now?
I found a good solution using adb shell
adb shell "am start -a android.media.action.STILL_IMAGE_CAMERA" && sleep 1 && adb shell "input keyevent 27"
Notice the action is STILL_IMAGE_CAMERA
Reference Android 4.4 won't allow me to save a picture when captured using adb commands
It is impossible.
You cannot control the behavior of the default camera app. Worse than that, you do not know what app will be used to fulfill your ACTION_IMAGE_CAPTURE intent. The end user has full power to install an alternative camera app, or may have malware installed that pretends to be a camera app (being a camera app means in this context that the manifest declares that it can perform ACTION_IMAGE_CAPTURE). But first of all, the ODMs preinstall camera apps that not necessarily behave the same way as the AOSP camera.
Such app may follow the contract for ACTION_IMAGE_CAPTURE intent response, but there is no guarantee. SO is full of questions about situations where the preinstalled camera app does not recognize extras correctly, or produces unexpected results.
Even if the result looks correct, there is no way for your app to know if the picture is actually being taken by the camera. It could be an image from the gallery, or a fake image, if the camera app chooses so.
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'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);