0

I have my READ_EXTERNAL_STORAGE permissions flagged in my Android Manifest. I have checked my Android phone (Android 7.0) and the file is definitely in the directory "/storage/emulated/0/Incoming/". However when I try to detect the file in Xamarin it says that it is not found. I do not receive an error since I check for it before trying to open it.

The string file shows a path and file name of "/storage/emulated/0/Incoming/item_list.csv" which is consistent to what I am looking for.

I have this installed on a ruggidized device and it works fine but for some reason when I want to test anything on my cellular Android device I get this issue.

I researched this on StackOverflow and most of the answers refereed to the path being incorrect. I changed path to include the AbsolutePath property but it did not work.

Thanks,

Mr.Rob

    private string[] Load_Item_File()
    {
        string[] _result = new string[1000]; 

        try
        {
            string path = Path.Combine((string)Android.OS.Environment.ExternalStorageDirectory, "Incoming");
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);

                _result[0] = "ERROR: DIRECTORY LOCKED";
                return _result;
            }

            string file = Path.Combine((string)path, "item_list.csv");
            if (!File.Exists(file))
            {

                _result[0] = "ERROR: FILE NOT FOUND";
                return _result;
            }

            _result = File.ReadAllLines(file,encoding:Encoding.ASCII);

            return _result;

        }
        catch (Exception ex)
        {
            string errorMessage = ErrorLog.CreateErrorMessage(ex);
            ErrorLog.LogFileWrite(errorMessage);

            _result[0] = "ERROR: ISSUE WHEN READING FILE";
            return _result;
        }
    }
Mr Rob
  • 59
  • 7
  • Could it be a permissions issue? Maybe the app needs permission to read from that directory – Omar Himada Apr 03 '18 at 16:59
  • Definitely would cause a problem but I have the READ_EXTERNAL_STORAGE flag set to true. I do not believe there is another permission I need to set. – Mr Rob Apr 03 '18 at 17:08
  • Isn't `storage/emulated` the internal storage, not external storage? – Omar Himada Apr 03 '18 at 17:09
  • No actually it is a public external folder. – Mr Rob Apr 03 '18 at 17:22
  • If I open my phone and go to `/storage/emulated/0` then I am looking at the internal storage of my device, not my external storage. Just FYI – Omar Himada Apr 03 '18 at 17:24
  • Ya it is misleading. Here is a blog that explains the differences between external and internal: https://kimsereyblog.blogspot.com/2016/11/differences-between-internal-and.html – Mr Rob Apr 03 '18 at 17:31
  • You also have to prompt your user to allow permission so even though it's allowed in the manifest the device may still not actually have permission. You can check this on your device by going to Settings>Apps>YourApp>Permissions and make sure the permission is turned on. If not I answered a possibly similar question [here](https://stackoverflow.com/questions/46911486/xamarin-system-unauthorizedaccessexception-access-to-the-path-is-denied/46933816#46933816) on how to prompt the user. – Nick Peppers Apr 03 '18 at 17:57
  • I checked permission on the device and it had the correct permissions for the application. I also went to your link and implemented your code example; it showed that it did have the permissions. I removed my file check and ran it and received this error: {System.IO.FileNotFoundException: Could not find file "/storage/emulated/0/Incoming/item_list.csv" Double checked to make sure the file was there and it was. Lastly I changed the version to run on Android 7.0 instead of 4.4 and still received the same error. – Mr Rob Apr 03 '18 at 18:46
  • If I was to look at this post I would say to myself, no way, the file location or name must be wrong. Here is a screenshot of its location on my device https://imageshack.com/a/img924/2061/Z2nKsG.png . – Mr Rob Apr 03 '18 at 18:58

0 Answers0