Save Asset into Android Internal Storage

AtanasAngelovAtanasAngelov Member ✭✭✭
edited September 19 in Xamarin.Android

Hiya, i am having some issues with saving a txt file into the internal storage where when i save it inside the storage, i would like to be able to interact it with it or just opening the my documents on the phone and actually finding the file, i have tried multiple methods but it only stores it somewhere where i cannot see it, is there a way to fix this?

This is what i am using, this method is for the database but i have a different method for a txt file they are the same way written

Best Answer

Answers

  • JarvanJarvan Member, Xamarin Team Xamurai

    i would like to be able to interact it with it or just opening the my documents on the phone and actually finding the file

    You can't read the internal file on your phone. Internal Storage is a portion of the file system that can be accessed only by the application or the operating system. Try to save the file to external storage, you'll get it on your phone easily.

    var filename = System.IO.Path.Combine("storage/emulated/0/Download/", "myfile.txt");
    
    using (var streamWriter = new StreamWriter(filename))
    {
        streamWriter.WriteLine("new file");
    }
    

    Tutorial:
    https://docs.microsoft.com/en-us/xamarin/android/platform/files/external-storage?tabs=windows

  • AtanasAngelovAtanasAngelov Member ✭✭✭


    I keep getting this error when i try to use your method but it gives me an error

  • JarvanJarvan Member, Xamarin Team Xamurai

    Have you added the external storage permissions?

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    
  • AtanasAngelovAtanasAngelov Member ✭✭✭

    Those have been added.

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭

    Hello all;

    @AtanasAngelov check the manifest file if the use permission has been write inside

  • AtanasAngelovAtanasAngelov Member ✭✭✭


    I would assume this is what you are looking for and yes i do have them enabled

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited September 19

    How did you test the code? An emulator device? Try to deploy the demo on a phone device.

  • AtanasAngelovAtanasAngelov Member ✭✭✭

    it is on a phone device, we have tried multiple diffferent phone devices but the file from the asset never appear into the phone, later on the app can access it but its not visible when i go through the phones files

  • JarvanJarvan Member, Xamarin Team Xamurai
    edited September 19

    Did the error still occur? Is there no file on the folder storage/emulated/0/Download/xx? It works on my side.

  • AtanasAngelovAtanasAngelov Member ✭✭✭

    The code that you gave still gives me an error, i do remove the app from my phone and re-deploying to make sure that i can do like a brand new deployment but it still says that the Access to the path is denied

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭

    @AtanasAngelov
    a possibility is perhpas if you use android 6 or more you need to run
    requestPermissions as something like this

    `
    string[] PermissionsRequest = { Manifest.Permission.WRITE external..., Manifest.Permission.READ external... };
    const int RequestId = 999;
    RequestPermissions(PermissionsRequest, RequestId);

    `
    there are a lot of sample on internet

  • AtanasAngelovAtanasAngelov Member ✭✭✭

    That is not the case as well, i have tried both of your solution and still it gives me the same error.

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭

    @AtanasAngelov is it possible for you to zip all your project and publish it or something where we can see what's happens because it's very strange ...

    thanks

  • AtanasAngelovAtanasAngelov Member ✭✭✭

    We do have a way to solve this issue as we have used it in a different project but basically we have a txt files which acts like a settings for the app, you can open it, change the configuration and stuff like that, that txt file with the database needs to be inside the phone device without me placing there manually, it has to be automation

  • JarvanJarvan Member, Xamarin Team Xamurai

    Try to use the following path, to avoid using hard code because some phones don't have the path storage/emulated/0/Download/.

    var filename = System.IO.Path.Combine((Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads)).ToString(), "reservation.txt");
    
    using (var streamWriter = new StreamWriter(filename))
    {
        streamWriter.WriteLine("new file");
    }
    
  • AtanasAngelovAtanasAngelov Member ✭✭✭

    Once again, Access to the path is denied, we have tried to use our method but it did not work i keep getting the same error

  • JarvanJarvan Member, Xamarin Team Xamurai

    Which version of the phone did you deploy on? Check the permission at runtime.

    private void CheckAppPermissions()
    {
        if ((int)Build.VERSION.SdkInt < 23)
        {
            return;
        }
        else
        {
            if (PackageManager.CheckPermission(Manifest.Permission.ReadExternalStorage, PackageName) != Permission.Granted
                && PackageManager.CheckPermission(Manifest.Permission.WriteExternalStorage, PackageName) != Permission.Granted)
            {
                var permissions = new string[] { Manifest.Permission.ReadExternalStorage, Manifest.Permission.WriteExternalStorage };
                RequestPermissions(permissions, 1);
            }
         }
    }
    

    Refer to:
    https://stackoverflow.com/questions/46911486/xamarin-system-unauthorizedaccessexception-access-to-the-path-is-denied

  • AtanasAngelovAtanasAngelov Member ✭✭✭

    When i ran the code that you send me, i checked the sdk that i am doing it as i am testing the app on Samsung Galaxy S9, it was 28 but we have made it so that mobiles phones that have Android 7.1 or around there to be able to run the app.

  • AtanasAngelovAtanasAngelov Member ✭✭✭

    I have been trying soo many ways to make this thing to work, i have tried to make a directory in the internal storage, it worked a few times but now it wont even create a file, i have tried to move the file from the external to internal storage into the newly created file but that did not work either, i have tried just saving the file inside the internal storage itself but once again that does not work, i am wondering what do i need to do to be able to move my database file from the assets folder which automatically goes into the phone when i run and then moving it to like a folder that i have created.

  • ChristopheBERNARDChristopheBERNARD BEMember ✭✭✭✭

    Hello all,
    @AtanasAngelov i can't understand why it doesn't work ...

    i have several app under android 6 and 7 and the only things we do for to write a file inside the storage it's request permission and add these permission to the manifest ..

    That's all

    for to advance, make perhpas a little application just with one activity on the oncreate event ask permission.
    add permission to the manifest add few code for to write an file

    and publish the zip file here

    thks

  • AtanasAngelovAtanasAngelov Member ✭✭✭

    The Solution was actually rather stupid as there are sooo many permissions that i needed to consider but the permission on the phone to be able for the app to store files in the external storage was the correct solution.

Sign In or Register to comment.