File.Exists always fails

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);


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.



  • StevenTheEvenStevenTheEven 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 USMember ✭✭

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

  • StevenTheEvenStevenTheEven 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 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 SEMember ✭✭✭

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

  • MichaelFenskeMichaelFenske 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 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 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 ;)


    Hello to all of you!
    I do have the same problem mentionned by MichaelFenske using the same code.

    I am also referring to this link

    However adding the following lines of code before the if...

    Java.IO.File dir = new Java.IO.File(
    Android.OS.Environment.DirectoryPictures), "CameraAppDemo");

    string[] filesNamesInDir = dir.List();

    I can get all the files names (.jpg) withing the SD card directory.

    If I take one of those files names called : fName = "Photo_Thursday20170323151005.jpg";
    then execute the following code

    var sdCardPath = Android.OS.Environment.ExternalStorageDirectory.Path;
    var imageFilePath = System.IO.Path.Combine(sdCardPath, dir.Name, fName);
    if (System.IO.File.Exists(imageFilePath) {
    . . .
    the if result is false

    Please note that the imageFilePath is : /storage/emulated/0/CameraAppDemo/Photo_Thursday20170323151005.jpg

    I would really appreciate your help

Sign In or Register to comment.