Get the url of the image from the gallery in the activityResult

protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{

        base.OnActivityResult(requestCode, resultCode, data);
        ImageViewFotoEnviar = FindViewById<ImageView>(Resource.Id.ImgViewImagenEnviarMs);
        VidViewMensaje = FindViewById<VideoView>(Resource.Id.VidViewVideoEnviarMs);

        if (resultCode == Result.Ok)
        {
            if (data != null)
            {
         //data = {Intent { dat=content://com.android.providers.media.documents/document/image:78724 flg=0x1 }}
                 //how to get the url address of the image with the data object?????

        string DataString = data.ToString();
                string extension = System.IO.Path.GetExtension(DataString);

                Glide.With(this).Load(data.Data).Into(ImageViewFotoEnviar);
            } 

Best Answer

  • EspectrosEspectros ✭✭
    Accepted Answer

    I was with this same question few days ago, lol.
    I have a Snippet check it out.

    public string SaveImagePath(Uri uri)
    {
    ContextWrapper contextWrapper = new ContextWrapper(Application.Context);
    //This field is a custom method instance i have to Manage the Shared Preferences, Dismiss it if you want.
    ConfigurationManager configuration = new ConfigurationManager(contextWrapper.GetSharedPreferences("livedisplayconfig", FileCreationMode.Private));
    //-----
    string doc_id = "";
    using (var c1 = Application.Context.ContentResolver.Query(uri, null, null, null, null))
    {
    c1.MoveToFirst();
    string document_id = c1.GetString(0);
    doc_id = document_id.Substring(document_id.LastIndexOf(":") + 1);
    }

            string path = null;
    
            // The projection contains the columns we want to return in our query.
            string selection = Android.Provider.MediaStore.Images.Media.InterfaceConsts.Id + " =? ";
            using (var cursor = Application.Context.ContentResolver.Query(Android.Provider.MediaStore.Images.Media.ExternalContentUri, null, selection, new string[] { doc_id }, null))
            {
                if (cursor == null) return path;
                var columnIndex = cursor.GetColumnIndexOrThrow(Android.Provider.MediaStore.Images.Media.InterfaceConsts.Data);
                cursor.MoveToFirst();
                path = cursor.GetString(columnIndex);
    

    //Make a log with the Path returned.
    Log.Info("Path is", path);
    //This field is for saving the Path to Shared Preferences.
    configuration.SaveAValue(ConfigurationParameters.imagePath, path);
    }
    return path;

        }
    

Answers

  • EspectrosEspectros Member ✭✭
    Accepted Answer

    I was with this same question few days ago, lol.
    I have a Snippet check it out.

    public string SaveImagePath(Uri uri)
    {
    ContextWrapper contextWrapper = new ContextWrapper(Application.Context);
    //This field is a custom method instance i have to Manage the Shared Preferences, Dismiss it if you want.
    ConfigurationManager configuration = new ConfigurationManager(contextWrapper.GetSharedPreferences("livedisplayconfig", FileCreationMode.Private));
    //-----
    string doc_id = "";
    using (var c1 = Application.Context.ContentResolver.Query(uri, null, null, null, null))
    {
    c1.MoveToFirst();
    string document_id = c1.GetString(0);
    doc_id = document_id.Substring(document_id.LastIndexOf(":") + 1);
    }

            string path = null;
    
            // The projection contains the columns we want to return in our query.
            string selection = Android.Provider.MediaStore.Images.Media.InterfaceConsts.Id + " =? ";
            using (var cursor = Application.Context.ContentResolver.Query(Android.Provider.MediaStore.Images.Media.ExternalContentUri, null, selection, new string[] { doc_id }, null))
            {
                if (cursor == null) return path;
                var columnIndex = cursor.GetColumnIndexOrThrow(Android.Provider.MediaStore.Images.Media.InterfaceConsts.Data);
                cursor.MoveToFirst();
                path = cursor.GetString(columnIndex);
    

    //Make a log with the Path returned.
    Log.Info("Path is", path);
    //This field is for saving the Path to Shared Preferences.
    configuration.SaveAValue(ConfigurationParameters.imagePath, path);
    }
    return path;

        }
    
  • JozsaGlezsJozsaGlezs Member ✭✭

    Thank you very much brother I managed to bring the path with this method just remove the part of the comment and the configuration.SaveAS because I do not know what library you are driving. Thank you very much :) :D :D :D :D :D :D :p B)

  • JozsaGlezsJozsaGlezs Member ✭✭

    Video path from Uri

    public string SaveImagePath(Uri uri)
    {
    ContextWrapper contextWrapper = new ContextWrapper(Application.Context);
    //This field is a custom method instance i have to Manage the Shared Preferences, Dismiss it if you want.
    //ConfigurationManager configuration = new ConfigurationManager(contextWrapper.GetSharedPreferences("livedisplayconfig", FileCreationMode.Private));
    //-----
    string doc_id = "";
    using (var c1 = Application.Context.ContentResolver.Query(uri, null, null, null, null))
    {
    c1.MoveToFirst();
    string document_id = c1.GetString(0);
    doc_id = document_id.Substring(document_id.LastIndexOf(":") + 1);
    }

            string path = null;
    
            // The projection contains the columns we want to return in our query.
            string selection = Android.Provider.MediaStore.Images.Media.InterfaceConsts.Id + " =? ";
            string selectionVideo = Android.Provider.MediaStore.Video.Media.InterfaceConsts.Id + " =? ";
            using (var cursor = Application.Context.ContentResolver.Query(Android.Provider.MediaStore.Video.Media.ExternalContentUri, null, selectionVideo, new string[] { doc_id }, null))
            {
                if (cursor == null) return path;
                var columnIndex = cursor.GetColumnIndexOrThrow(Android.Provider.MediaStore.Images.Media.InterfaceConsts.Data);
                var columnIndexVideo = cursor.GetColumnIndexOrThrow(Android.Provider.MediaStore.Video.Media.InterfaceConsts.Data);
                cursor.MoveToFirst();
                path = cursor.GetString(columnIndexVideo);
    
                //Make a log with the Path returned.
                Log.Info("Path is", path);
                //This field is for saving the Path to Shared Preferences.
                //configuration.SaveAValue(ConfigurationParameters.imagePath, path);
            }
            return path;
        }
    
Sign In or Register to comment.