File.Exists always fails

MichaelFenskeMichaelFenske Michael FenskeUSMember

I'm playing with an app that will load images from an sd card. I am running in debug mode in VS 2012 and running on a Motorola Xoom tablet (Android 4.1.2). I have placed the images at the root of the card and in a subdirectory called Images. I have tried a couple of different tweaks to the code but essentially .Exists always fails (no exception though).

` var sdCardpath = Android.OS.Environment.ExternalStorageDirectory.Path; var imageFilePath = System.IO.Path.Combine(sdCardpath, "Plate1.jpg");

            // File imgFile = new File(imageFilePath);
            // if (imgFile.Exists())

            if (System.IO.File.Exists(imageFilePath))
            {
                var imageFile = new Java.IO.File(imageFilePath);

                Bitmap myBitmap = BitmapFactory.DecodeFile(imageFile.AbsolutePath);

                ImageView myImage = (ImageView)FindViewById<ImageView>(Resource.Id.imageView1);
                myImage.SetImageBitmap(myBitmap);
            }

`

I tried deploying the app to the tablet and it still fails to load the images. I have turned on WRITE_EXTERNAL_STORAGE in the manifest. I have used the SanDisk app to verify the files are there and that the default viewer can open them. I'm sure its something simple just not sure where to look.

Thanks

Posts

  • StevenTheEvenStevenTheEven Michael Andersson SEMember ✭✭✭

    If you have placed the files in "subdirectory called Images" why doesn't you add that to your path? var imageFilePath = System.IO.Path.Combine(sdCardpath, "Images\Plate1.jpg");

  • MichaelFenskeMichaelFenske Michael Fenske USMember

    I tried that (actually "Images/Plate1.jpg")

  • StevenTheEvenStevenTheEven Michael Andersson SEMember ✭✭✭

    K, then try to write a file to the dir you want to read from and see if a file is created there.

  • MichaelFenskeMichaelFenske Michael Fenske USMember

    var sdCardpath = Android.OS.Environment.ExternalStorageDirectory.Path;
                    var writeFileName = System.IO.Path.Combine(sdCardpath, "Images/Readme.txt");
                    System.IO.File.WriteAllText(writeFileName, "testing 1 2 3");

    The added code generates the following exception:

    • ex {System.IO.DirectoryNotFoundException: Could not find a part of the path "/storage/sdcard0/Images/Readme.txt". at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean anonymous, FileOptions options) [0x00000] in :0 at System.IO.FileStream..ctor (System.String path, FileMode mode, FileAccess access, FileShare share) [0x00000] in :0 at System.IO.StreamWriter..ctor (System.String path, Boolean append, System.Text.Encoding encoding, Int32 bufferSize) [0x00000] in :0 at System.IO.StreamWriter..ctor (System.String path, Boolean append, System.Text.Encoding encoding) [0x00000] in :0 at System.IO.File.WriteAllText (System.String path, System.String contents, System.Text.Encoding encoding) [0x00000] in :0 at System.IO.File.WriteAllText (System.String path, System.String contents) [0x00000] in :0 at PlateViewer.Activity1.DisplayData () [0x00130] in c:\src\VS2012\Projects\Hello_World\PlateViewer\Activity1.cs:93 } System.IO.DirectoryNotFoundException
  • StevenTheEvenStevenTheEven Michael Andersson SEMember ✭✭✭

    What permissions do you have on your Images dir? Perhaps try another dir or even the root-dir?

  • MichaelFenskeMichaelFenske Michael Fenske USMember

    Removing "Images" from "Images/Readme.txt" allowed the code to continue without an exception but I can't find the file that should have been created.

  • MichaelFenskeMichaelFenske Michael Fenske USMember

    The ReadMe.Txt file appears using the SanDisk tool in internal storage (not exactly sure where) as file explorer from Windows can't seem to find it. Not sure how to check or set permissions for the operation but I'm suspecting that the micro sd card is not being seen properly?

  • StevenTheEvenStevenTheEven Michael Andersson SEMember ✭✭✭

    Start and use an avd-image and see if it works then or not. With that you can use "Android SDK Tools" and monitor.bat to browse your structure/files to see what is created and/or not. To narrow it down if it's your code or if it's your motorola xoom tablet that is fucked up ;)

Sign In or Register to comment.