Camera Intent not working with multiple camera apps - android

In my app I run camera intent to take picture with photo App, with this code below:
Activity activity;
File currentPhotoFile;
Intent capturePhoto = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri uri = GenericFileProvider.getUriForFile(activity, activity.getPackageName() + providerSufixName, currentPhotoFile);
capturePhoto.putExtra(MediaStore.EXTRA_OUTPUT, uri);
activity.startActivityForResult(capturePhoto, REQUEST_PHOTO);
And after that I catch the created photo in my activity.onActivityResult function in Activity
If I have only one! photo App on my mobile device - everything works fine.
But! If I installed on my device one more! photo application - the code above is not working :(
In case two photo apps, when I call activity.startActivityForResult - I see andoid system dialog with list of all available photo apps. (like Intent.createChooser calling). I select from it, preferable photo app and tap "JUST ONCE". Selected application runs, I take picture inside it and press Ok button for returning back to my App.
At this moment I recieve callback to my onActivityResult with my code: REQUEST_PHOTO but with requestCode = RESULT_CANCELED. And that's all :(
Why it's not working?
And how I can resolve this issue and use camera intent with more than one photo application?

The problem was that in manifest I have option:
android:launchMode="singleInstance" for my activity.
<activity
android:name="MyActivity"
android:launchMode="singleInstance"
>
For solving above issue you should remove singleInstance option from manifest file (or replace it within singleTask option).
This is resolve the issue and onActivityResult is properly working.

Related

Android / Xamarin, Open file with intent and close app after back button pressed

I have a problem with a feature of my application.
I need to open external files to display them like .jpeg or .xls for example. For that I use Intents to open them with the default system application and it works fine.
But in a case where we will first open a document, for example a photo. this will display in the default app and then I go back and forth in my app. Everything is OK
But then I'm going to open another standard .xls document, it'll open the document in the appropriate application and going back will show the opened image first and not my application again.
I don't understand why it happens like this.
What I would like is that when we do previous in a file viewer app, the app quits and doesn't stay in the ActivityManager.
Is it possible ?
I have already tried some intent flags but without success.
My code here :
Android.Net.Uri fileUri = FileProvider.GetUriForFile(Android.App.Application.Context, "com.app.fileprovider"
, file);
Intent intent = new Intent(Intent.ActionView);
intent.SetData(fileUri);
intent.AddFlags(ActivityFlags.GrantReadUriPermission);
intent.AddFlags(ActivityFlags.NewTask);
intent.AddFlags(ActivityFlags.NoHistory);
Android.App.Application.Context.StartActivity(intent);
return true;
Thanks a lot for the help
Max B
SO !
My bad, It was not a real problem.
It was a design error.
Indeed during my call of my function to call the intention I linked my opening event without then unbinding it. So that with each call I recalled X times my file opening function without ever destroying my variable or my variable instance.
The Intent Works Well
Thanks for the comments

Activity result code always 0 (cancel) when there is two or more activities to handle intent

I have a problem, when I startActivityForResult to take picure, resultCode is always Activity.RESULT_CANCELED (0) if there is two or more camera apps that can handle that intent (app chooser appears). But if I set one of them as default app and next time it doesn't offer me a chooser, everything works fine, and it takes picture and detects it in a onActivityResult in my fragment that started startActivityForResult().
Same thing happens when I try to open gallery to select picture. If there are two gallery apps, I choose either one of them, picture selection result is always 0.
This is the code I use to start camera app:
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(takePictureIntent, AppConstants.TAKE_PICURE_CAMERA_REQUEST_CODE);
}
I had the same issue, when my activity has launchMode="singleTask", but I change to singleTop and that solve the problem
I just updated my Poco X3 to Android 11 and now I have the same problem... It's probably a bug in MIUI (12.0.8). It works fine on other Android 11 devices.
Changing launchMode does not work. However when I detect a MIUI user I circumvent the chooser and just open the first camera app it finds.
Worst thing is that you can't really detect when this issue occurs. So you can't log when it goes wrong and therefore you can't find out which users are experiencing this issue.
The same issue also occurs when you try to get a photo from a gallery, or when you try to open a photo in a photo app. The latter results in a 'cannot find photo' message.

How to open system camera in its normal operation after I launch it from my activity?

I am working with camera in project and provided a simple button in activity which will open camera app in my device. But the problem is after clicking one photo, it asks weather I want to keep my picture or not and after clicking yes, it return to my main activity.
But I want to operate camera in normal mode, I mean like when we click photo after photos and all that.
My code
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, 0);
following permission is used
uses-permission android:name="android.permission.CAMERA" uses-permission
I dont know why tags are not working.
Any kind of help is appreciated!
To take multiple photos, you should launch the intent with this action:
Intent intent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
startActivity(intent);

Custom Action Intent Opens my App in the same context as the app that I clicked From

I have searched a lot on this.
But, I have my code working to open my app using a custom intent URL. This URL is usually delivered via email.
When I click on the link, it opens my app fine and everything seems to be working; however, it opens in the context of the email application.
For example, if I click on the link from Gmail, when I open multitasking, I have to click on Gmail to return back to the app that just opened.
I would think it should open my app and I can continue using Gmail while my other app is running.
Any thoughts on this?
Make your URL look like this:
intent:#Intent;launchFlags=0x10000000;component=com.mycompany.myapp/com.mycompany.myapp.MyActivity;end
This URL contains the launchFlag for Intent.FLAG_ACTIVIY_NEW_TASK, so this will launch your app in a separate task (outside of the email client or browser or whatever).
EDIT: Add additional details based on OP's comment
You say that you are using a URL like this: http://com.my.app/5058749
In that case you must have used an Intent filter to get Android to open your app by specifying an <intent-filter> on a certain <activity> in your manifest. There are several things you can do to deal with the problem of the launched Activity ending up in the same task as the Activity that launched it:
1) If the Activity is always intended to be the root (starting, first) Activity of a task, you can put the following code in onCreate() after the call to super.onCreate():
if (!isTaskRoot()) {
// Activity was launched into another task. Restart it in its own task
Intent intent = new Intent(this, this.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
return;
}
2) You can set the launch mode of this Activity to singleTask in the manifest by adding
android:launchMode="singleTask"
to the <activity> definition. This will cause the Activity to be launched in its own task but this launch mode has other consequences that are more subtle and so you need to be careful about using it. In general I don't like to suggest this because it tends to create more problems than it solves.
3) You can determine if your app was launched from the browser or email client by examining the Intent used to start it in onCreate() (The Intent will have the data set to the URL when launched via the browser or email client). You can then decide if you want to restart it in its own task by using the code I've supplied in option 1 above.
Add Intent.FLAG_ACTIVITY_NEW_TASK and Intent.FLAG_ACTIVITY_CLEAR_TOP flags(intent.SetFlags() to your intent. Your actitivty will be opened in a new task and this activity will be the root of your new stack.
This is the default behavior with android, but to override it, you need to pass
Intent.FLAG_ACTIVITY_NEW_TASK
Intent.FLAG_ACTIVITY_CLEAR_TOP
with your intent.

Activity Camera (in application Camera) is not responding

I've an application that opens activity camera with the following code:
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(Environment.getExternalStorageDirectory() + "/" + image)));
activity.startActivityForResult(intent, CAMERA_ACTIVITY_OK);
The problem is that the camera application, after some "snapshots", returns the following error:
Activity Camera (in application Camera) is not responding.
After that, the camera application doesn't works anymore.
This problem happens only with Android 2.3 version
Could you help me please?
Thanks
Snapshot"s"? As in more than one? That intent should only produce one shot, or the user can cancel the shot (producing no image). Are you calling the intent multiple times? If so, make sure the target image file (EXTRA_OUTPUT) is different each time. I probably can't help you without seeing more of your code or the error log.
BTW: Using the Camera app with Intents is now covered in the Android Developer documentation:
http://developer.android.com/guide/topics/media/camera.html#intents

Categories

Resources