Get path of selected image from gallery in Android

vkt_rbvkt_rb USMember ✭✭✭

Hi everyone, I'm trying to load a selected image from the gallery to a Image in xaml, at this moment I just only could to open the file explorer and select the image but I don't know how to get and save the path of the image selected.. this is the code to select the image.

    private loadAvatar()
        {
            try
            {
                var imageIntent = new Intent(Intent.ActionPick);
                imageIntent.SetType("image/*");             
                imageIntent.SetAction(Intent.ActionGetContent);
                ((MainActivity)Forms.Context).StartActivityForResult(
                    Intent.CreateChooser(imageIntent, "Select a picture"), 1000);

            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

As you can see I only could onpen the explorer but I don't know to get the path of the selected file.

Posts

  • AdrianKnightAdrianKnight USMember ✭✭✭✭
  • vkt_rbvkt_rb USMember ✭✭✭

    Hi @AndrianKnight, yes I've try to use the MediaPlugin but isn't working for me... I've couldn't to save the path after of select the image

  • AdrianKnightAdrianKnight USMember ✭✭✭✭

    @vkt_rb Which version of the library were you using? It requires WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE & CAMERA permissions, but usually it should be able to set those for you.

    I just tested it, and it's working for me.

    Install it in both PCL and Android projects. (For, iOS it should be installed as well if you are going to use it there).

    There were my packages:

      <package id="Xam.Plugin.Media" version="2.6.0.4-beta" targetFramework="portable45-net45+win8+wp8+wpa81" />
      <package id="Xamarin.Forms" version="2.2.0.45" targetFramework="portable45-net45+win8+wp8+wpa81" />
    

    See sample Usage code: https://github.com/jamesmontemagno/MediaPlugin

  • vkt_rbvkt_rb USMember ✭✭✭
    edited October 2016

    Hi @AdrianKnight thanks for the reply, I'm using the same version of labrary, but with that sample I have a Problem because I can't run it, it just not run in my emulators, so I can't debbug the code to see it and try to apply in my solution. I'm trying look for the part of pick a image but I think that I'm loosing one part because when I try to apply the code in my solution it don't give the path of the image

  • vkt_rbvkt_rb USMember ✭✭✭

    I'm working with Xamarin Studio 6.1.1 ... and all the samples that I've downloaded of this topic runned without problems but this one not run in the emulators

  • AdrianKnightAdrianKnight USMember ✭✭✭✭

    Can you run the sample on your device (not emulators)?

  • vkt_rbvkt_rb USMember ✭✭✭

    I 've not tried it because I would like to debbug and see the process of the sample in the part of picking the image and getting the path in the device..

  • vkt_rbvkt_rb USMember ✭✭✭

    Thanks for the help @AdrianKnight, Have you in your solution the part of the code when it get the path of the selected image?

  • AdrianKnightAdrianKnight USMember ✭✭✭✭

    I usually never bother with emulators. I develop on the physical device and then test emulators before release. Deploying to your device should be faster anyway. You shouldn't worry about image paths.

    image.Source = ImageSource.FromStream(() =>
        {
            var stream = file.GetStream();
            file.Dispose();
            return stream;
        }); 
    

    What you need is the image source. This code sets the Source property of your image with a stream. Then your image should be ready for display.

  • vkt_rbvkt_rb USMember ✭✭✭

    In that case what became file ?

  • AdrianKnightAdrianKnight USMember ✭✭✭✭

    TakePhotoAsync returns a media file:

    /// <summary>
    /// Take a photo async with specified options
    /// </summary>
    /// <param name="options">Camera Media Options</param>
    /// <returns>Media file of photo or null if canceled</returns>
    Task<MediaFile> TakePhotoAsync(StoreCameraMediaOptions options);
    

    so file is your photo. Now you just need to do file.GetStream() to get the data and hook it up to Source of image.

  • JamesMontemagnoJamesMontemagno USForum Administrator, Xamarin Team, Developer Group Leader Xamurai

    The Media file also returns the location of the file on disc for each platform.\

    It is all documented in the github repo

  • vkt_rbvkt_rb USMember ✭✭✭

    Thanks a lot @AdrianKnight I could solve the problem using the Media Plugin I shared my code for the community

    Image img_perfil = new Image();
    
    var file = await CrossMedia.Current.PickPhotoAsync();
        if (file == null)
            return;
    
        var im = ImageSource.FromStream(() =>
        {
            var stream = file.GetStream();
            //file.Dispose();
            return stream;
        });
    
        img_perfil.Source = im;
    
  • vkt_rbvkt_rb USMember ✭✭✭
    edited October 2016

    @AdrianKnight said:
    TakePhotoAsync returns a media file:

    /// <summary>
    /// Take a photo async with specified options
    /// </summary>
    /// <param name="options">Camera Media Options</param>
    /// <returns>Media file of photo or null if canceled</returns>
    Task<MediaFile> TakePhotoAsync(StoreCameraMediaOptions options);
    

    so file is your photo. Now you just need to do file.GetStream() to get the data and hook it up to Source of image.

    Thanks

  • SaiLokeshSaiLokesh INMember ✭✭
    edited March 2017

    Trying to get the path of the selected image,
    Help me please
    Thanks,

    var imageIntent = new Intent();
                imageIntent.SetType("image/*");
                imageIntent.SetAction(Intent.ActionGetContent);
                StartActivityForResult(
                    Intent.CreateChooser(imageIntent, "Select photo"), 0);
    protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
            {
                base.OnActivityResult(requestCode, resultCode, data);
                if (resultCode == Result.Ok)
                {
                    //var imageView =
                    //    FindViewById<ImageView>(Resource.Id.imageView1);
                    //imageView.SetImageURI(data.Data);
    
            string pathofpicked=data.Data.Path;
            //here trying to get the path of selected image but I'm not getting it,
                    Bitmap propic = BitmapFactory.DecodeFile(pathofpicked);
                    ProfileActivity pa = new ProfileActivity();
                    Bitmap resized = pa.resizeAndRotate(propic, 450, 450);
                    try
                    {
            string Insertpath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
                        var filePath = System.IO.Path.Combine(Insertpath);
                        var stream = new FileStream(filePath, FileMode.Create);
                        resized.Compress(Bitmap.CompressFormat.Jpeg, 100, stream);
                        stream.Close();
                    }
                    catch (Exception ex)
                    {
    
                    }
    
    
                }
    
    
            }
    
  • kalkotekedarkalkotekedar USMember ✭✭

    @JamesMontemagno said:
    The Media file also returns the location of the file on disc for each platform.\

    It is all documented in the github repo

    I have read your documentation and use both method file.path and file.AlbumPath, but this not giving me the local file path of the selected image.

Sign In or Register to comment.