Get path to pictures directory

18,150

Solution 1

I use this code for devices:

path = Android.OS.Environment.GetExternalStoragePublicDirectory(
  Android.OS.Environment.DirectoryPictures).AbsolutePath;

string myPath= Path.Combine(path, "file.name");

For emulators, it does not work.

Solution 2

Please use following code...

private void CreateDirectoryForPictures()
    {
        App._dir = new File(
            Environment.GetExternalStoragePublicDirectory(
                Environment.DirectoryPictures), "imgen");
        if (!App._dir.Exists())
        {
            App._dir.Mkdirs();
        }
    }

And this CreateDirectoryForPictures() function call. Then Created folder imgen.

Share:
18,150
Freshchris
Author by

Freshchris

Student from the TU Munich. Dedicated to modern technologies.

Updated on August 17, 2022

Comments

  • Freshchris
    Freshchris over 1 year


    I try to save an Image (Bitmap/byte[]) with my Xamarin.Android app.
    I used

        private string getPathToFile(string fileName)
        {
            File dir = new File(Environment.GetExternalStoragePublicDirectory(Environment.DirectoryPictures), "imgen");
            if (dir.Exists())
            {
                dir.Mkdirs();
            }
    
            File image = new File(dir, fileName);
            return image.Path;
        }
    

    So the returned path looks something like this:

    "/storage/emulated/0/Pictures/imgen/new.png"
    

    But this path does not exist on the emulator as I checked with the Android Device Monitor.
    I read that this folder is some kind of link to a mnt/shell/emulated/... folder, which actually exists on the emulator.
    But how can I retrieve this real path in my Application?

  • Freshchris
    Freshchris over 7 years
    This works. The Problem was that the folder "imgen" wasn't created.
  • Abandoned Cart
    Abandoned Cart over 4 years
    Environment.DIRECTORY_PICTURES