Forum Libraries, Components, and Plugins

Media.Plugin on android

I'm not an android user. I've got the plugin working. I take a picture. however it just stays on camera app. How do I get it to be done so that it returns to my app after I take a picture?

Answers

  • jezhjezh Member, Xamarin Team Xamurai

    You can use DependencyService to open another app through URI in xamarin forms.
    First, we can create an interface for the OpenAppService, then create a Launch() method that we’ll call later:

    public interface IOpenAppService
    {
        Task<bool> Launch(string stringUri);
    }
    

    The next step is to create platform-specific implementations for Android, and iOS.

    [assembly: Xamarin.Forms.Dependency(typeof(OpenAppService))]
    namespace OpenAppLaunch.Droid
    {
        public class OpenAppService : Activity, IOpenAppService
        {
            public Task<bool> Launch(string stringUri)
            {
            try{
                Intent intent = Android.App.Application.Context.PackageManager.GetLaunchIntentForPackage(stringUri);
    
    
                if (intent != null)
                {
                    intent.AddFlags(ActivityFlags.NewTask);
                    Forms.Context.StartActivity(intent); 
                }
                else
                {
                    intent = new Intent(Intent.ActionView);
                    intent.AddFlags(ActivityFlags.NewTask);
                    intent.SetData(Android.Net.Uri.Parse("market://details?id=" + stringUri)); // This launches the PlayStore and search for the app if it's not installed on your device
                    Forms.Context.StartActivity(intent);
                }
                    return Task.FromResult(true);
              }
                return Task.FromResult(false);
            }
    

    In IOS

    File info.plist

    <key>LSApplicationQueriesSchemes</key>
    <array>
        <string>google</string>
        <string>facebook</string>
    </array>
    

    OpenAppService (iOS)

    [assembly: Xamarin.Forms.Dependency(typeof(OpenAppService))]
    namespace OpenAppLaunch.iOS
    {
        public class OpenAppService : IOpenAppService
        {
    
            public Task<bool> Launch(string stringUri)
            {
                try
                {
                    NSUrl request = new NSUrl(stringUrl);
                    bool isOpened = UIApplication.SharedApplication.OpenUrl(stringUrl);
    
                    if (isOpened == false)
                        UIApplication.SharedApplication.OpenUrl(new NSUrl(stringUrl));
    
                    return Task.FromResult(true);
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Cannot open url: {0}, Error: {1}", request.AbsoluteString, ex.Message);
                    var alertView = new UIAlertView("Error", ex.Message, null, "OK", null);
    
                    alertView.Show();
    
                    return Task.FromResult(false);
                }
            }
        }
    

    Refer : https://mindofai.github.io/Launching-Apps-thru-URI-with-Xamarin.Forms/

  • jezhjezh Member, Xamarin Team Xamurai

    @bug0926 hey friend, have you resolved your question?

Sign In or Register to comment.