Home > Mobile >  Storing data in local folder is limited
Storing data in local folder is limited

Time:03-23

I am trying to save data locally to my device app folder.

When I try to save collected data on an actual Android smartphone, it doesn't work. It is limited by name and filetype, as I cannot change it from test.txt and it is limited in string length, as a maximum of twelve characters get saved.

I have the acquired the following permissions:

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

This is my code:

MainPage.xaml.cs

private async void ExportData(object sender, EventArgs e)
    {
        var items = await App.Database.GetDataAsync();
        DependencyService.Get<IFileService>().CreateFile(items);
    }

Interface

using System;
using System.Collections.Generic;
using System.Text;

namespace LocationApp.Interface
{
    public interface IFileService
    {
        void CreateFile(List<LocationData> items);
    }
}

Service

using Android.App;
using LocationApp.Droid;
using LocationApp.Interface;
using System.Collections.Generic;
using System.IO;

[assembly:Xamarin.Forms.Dependency(typeof(FileService))]
namespace LocationApp.Droid
{
    public class FileService : IFileService
    {
        public string GetRootPath()
        {
            return Application.Context.GetExternalFilesDir(null).ToString();
        }

        public void CreateFile(List<LocationData> items)
        {
            var fileName = "test-file.txt";
            var destination = Path.Combine(GetRootPath(), fileName);
            string[] text = new string[items.Count];

            for (int i = 0; i < text.Length; i  )
            {
                text[i] = $"{items[i].Latitude},{items[i].Longitude},{items[i].Day},{items[i].Time}";
            }

            File.WriteAllLines(destination, text);
        }
    }
}

I also attempted to see what would happen to an emulator, I used a Pixel 2 with Android 9.0, API 28 where I got the following error: [ContextImpl] Failed to ensure /storage/120E-0B1B/Android/data/com.companyname.locationapp/files: java.lang.IllegalStateException: Failed to prepare /storage/120E-0B1B/Android/data/com.companyname.locationapp/files/: android.os.ServiceSpecificException: (code -13)

In the end, I only care about putting all my data in a single file. The filename or the error on my emulator I provided in case the error is based on that. If not, I do not care if they are fixed/fixable.

CodePudding user response:

Based on your code, I created a simple demo, and it works on my android emulator(android 11) .

You can test on your side.

The code is:

    public void CreateFile(List<LocationData> items)
    {
        var fileName = "test-file.txt";

        var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);

        var destination = Path.Combine(documentsPath, fileName);
        string[] text = new string[items.Count];

        for (int i = 0; i < text.Length; i  )
        {
            text[i] = $"{items[i].Latitude},{items[i].Longitude}";
        }

        File.WriteAllLines(destination, text);
    }

And after I saved the data,I could get the saved data by the following code(the filename is test-file.txt):

    public string ReadData(string filename)
    {
        var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
        var filePath = Path.Combine(documentsPath, filename);
        return File.ReadAllText(filePath);
    } 
  • Related