Folders Xamarin Android Android Xamarin

Nov 30th, 2016 - written by Kimserey with .

When using Xamarin.Forms and Xamarin.Android, chances are that you had at one point of time to access a local file. Whether it is an image or a text file or even a local sqlite database, accessing a file from a Xamarin.Android project can be confusing. Today I will show you how you can get the absolute path of the different folders available in Xamarin.Android and we will see how we can get the paths from Xamarin.Forms.

  1. Internal folder
  2. Public and private external folders
  3. Access folder paths from Xamarin.Forms

1. Internal folder

The internal folder is a folder specific to your application. It can only be access within your application and no other applications can touch the files within that folder.

You can get it using the following:

1
Android.App.Application.Context.FilesDir.AbsolutePath

The path looks something like that:

1
/data/user/0/com.kimserey.myapp/files

Use this storage to store document specific to your application which will not be accessible from else where. Another important point is that when the user uninstall the application, all the data within that folder will be removed.

2. Public and private external folders

The external folder is a folder accessible by anyone. It can be mounted on a computer and any files is publicly available. Also any app with EXTERNAL_STORAGE read/write access will be able to access and modify these files.

There are two type of external folders, public and private. The difference beeing that private files will get deleted when your app is uninstall.

Private files on external folders are still accessible by anyone.

The public external folder can be accessed using:

1
Android.OS.Environment.ExternalStorageDirectory.AbsolutePath

The public external folder path path looks something like that:

1
/storage/emulated/0/

And the private external folder can be accessed using:

1
Application.Context.GetExternalFilesDir(null).AbsolutePath

The private external folder path looks something like that:

1
/storage/emulated/0/Android/data/com.kimserey.myapp/files

Note that the private folder is almost similar to the internal folder path apart from the fact

3. Access folder paths from Xamarin.Forms

To access folder paths from Xamarin.Forms we will need to create a service and get it through the dependency service.

To do that we can create an interface IPathService in the Xamarin Forms project.

1
2
3
4
5
6
public interface IPathService 
{
    string InternalFolder { get; }
    string PublicExternalFolder { get; }
    string PrivateExternalFolder { get; }
}

And in the Android project we can add the implementation and add the dependency on the assembly level.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
[assembly: Dependency(typeof(MyApp.PathService))]
namespace MyApp
{
    public class PathService: IPathService
    {
        public string InternalFolder
        {
            get 
            { 
                return Android.App.Application.Context.FilesDir.AbsolutePath;
            }
        }

        public string PublicExternalFolder
        { 
            get
            {
                return Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
            }
        }

        public string PrivateExternalFolder
        {
            get 
            {
                return Application.Context.GetExternalFilesDir(null).AbsolutePath; 
            }
        }
    }
}

We can then access the path from anywhere in our app using:

1
DependencySerivce.Get<IPathService>().InternalFolder

Conclusion

Today we learnt the different type of folder available in Android. Internal vs external and public external vs private external. Use internal folder if you want everything to be only contained in your app and prevent other apps from messing with your content. Use external to save file which are not application lifecycle threatening, photos taken from your app our good example. Bear in mind that any content stored in external folders can be removed by the user, even the external storage might not be always available. I hope this clear up the differences! Thanks for reading my post, if you have any questions leave it here or hit me on Twitter @Kimserey_Lam. Oh and don’t forget to support me by checking out my app Baskee. See you next time!

Other post you will like!

Designed, built and maintained by Kimserey Lam.