Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

How can you save data to a file inside the Mobile Phone? Xamarin Android

$
0
0

Well, the question is that, what exactly i'm doing wrong?

i saw another similar post here about it, but none of them actualy help me to fix the issue.

here is the code:

AndroidManifest.xml (Permissions):

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="Notificaciones_Movil.Notificaciones_Movil" android:versionCode="1" android:versionName="1.0" android:installLocation="auto">
    <uses-sdk android:minSdkVersion="15" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <application android:label="Notificaciones_Movil"></application>
</manifest>

Methods involved in save and read data (Xamarin Android C#):

private bool JSONDesdeArchivo(out ItemLogin loginLogan)
        {
            //deserialize JSON from file
            loginLogan = null;
            bool isFileExisting = false;
            try
            {
                string JSONstring = File.ReadAllText(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal));
                isFileExisting = true;

                if (!string.IsNullOrWhiteSpace(JSONstring))
                {
                    loginLogan = JsonConvert.DeserializeObject<ItemLogin>(JSONstring);
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                if (isFileExisting)
                {
                    Toast.MakeText(this, "Something go wrong: " + ex.ToString(), ToastLength.Long);
                }
                return false;
            }
        }

        private bool JSONHaciaArchivo(ItemLogin loginLogan)
        {
            //serialize JSON to file
            try
            {
                //output JSON file
                string outputJSON = JsonConvert.SerializeObject(loginLogan);
                File.WriteAllText(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), outputJSON);
                return true;
            }
            catch (Exception ex)
            {
                Toast.MakeText(this, "Something go wrong: " + ex.ToString(), ToastLength.Long);
                return false;
            }
        }

        private bool DeleteTextArchivo()
        {
            //serialize JSON to file
            try
            {
                //output JSON file
                File.WriteAllText(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "");
                return true;
            }
            catch (Exception ex)
            {
                Toast.MakeText(this, "Something go wrong: " + ex.ToString(), ToastLength.Long);
                return false;
            }
        }

Error that is showing to me:

{System.UnauthorizedAccessException: Access to the path '/data/data/Notificaciones_Movil.Notificaciones_Movil/files' is denied.
at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) [0x000f4] in <3fd174ff54b146228c505f23cf75ce71>:0
at System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.IO.FileOptions options, System.String msgPath, System.Boolean bFromProxy, System.Boolean useLongPath, System.Boolean checkHost) [0x00000] in <3fd174ff54b146228c505f23cf75ce71>:0
at (wrapper remoting-invoke-with-check) System.IO.FileStream:.ctor (string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int,System.IO.FileOptions,string,bool,bool,bool)
at System.IO.StreamWriter.CreateFile (System.String path, System.Boolean append, System.Boolean checkHost) [0x0001c] in <3fd174ff54b146228c505f23cf75ce71>:0
at System.IO.StreamWriter..ctor (System.String path, System.Boolean append, System.Text.Encoding encoding, System.Int32 bufferSize, System.Boolean checkHost) [0x00055] in <3fd174ff54b146228c505f23cf75ce71>:0
at System.IO.StreamWriter..ctor (System.String path, System.Boolean append, System.Text.Encoding encoding, System.Int32 bufferSize) [0x00000] in <3fd174ff54b146228c505f23cf75ce71>:0
at System.IO.StreamWriter..ctor (System.String path, System.Boolean append, System.Text.Encoding encoding) [0x00000] in <3fd174ff54b146228c505f23cf75ce71>:0
at (wrapper remoting-invoke-with-check) System.IO.StreamWriter:.ctor (string,bool,System.Text.Encoding)
at System.IO.File.WriteAllText (System.String path, System.String contents, System.Text.Encoding encoding) [0x00000] in <3fd174ff54b146228c505f23cf75ce71>:0
at System.IO.File.WriteAllText (System.String path, System.String contents) [0x00007] in <3fd174ff54b146228c505f23cf75ce71>:0
at Notificaciones_Movil.MainActivity.JSONHaciaArchivo (Notificaciones_Movil.ItemLogin loginLogan) [0x00009] in C:\Users\Sincron\Documents\Visual Studio 2015\Projects\Notificaciones-Movil\Notificaciones-Movil\MainActivity.cs:260 }

Any question, suggestion, comment, request of clarification etc than you do would be greatly apreciated too if constructive and helps to find the answer.

Thanks in advance


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>