L
L
LoliDeveloper2020-08-20 08:15:38
Android
LoliDeveloper, 2020-08-20 08:15:38

How to save serialized objects on the user's device in JSON format in an android application on Xamarin?

I'm trying to do it with simple methods, but it says "Access to the path /data.json is denied".
How can I bypass protection?
Made it a regular entry
File.WriteAllText("data.json", serializeString);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Korotenko, 2020-08-20
@LoliDeveloper

1 add permission to manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

https://docs.microsoft.com/en-us/xamarin/android/p...
Getting the path in the shared code
public static string DbPath => DependencyService.Get<ISqLite>().GetDatabasePath("user.db");

Implementation for android, for other platforms a little differently
#region License
// Разработано: Коротенко Владимиром Николаевичем (Vladimir N. Korotenko)
// email: [email protected]
// skype:vladimir-korotenko 
// https://vkorotenko.ru
// Создано:  09.04.2019 22:38
#endregion
using System;
using System.IO;
using SpecialForcesDirectory.Code;
using SpecialForcesDirectory.Droid;
using Xamarin.Forms;

[assembly: Dependency(typeof(SQLite_Android))]
namespace SpecialForcesDirectory.Droid
{
    public class SQLite_Android : ISqLite
    {
        public SQLite_Android() { }
        public string GetDatabasePath(string sqliteFilename)
        {
            string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            var path = Path.Combine(documentsPath, sqliteFilename);
            return path;
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question