简体   繁体   中英

Can't access file in Android External Storage

I'm using Xamarin Forms for a photo capture app and using James' Media Plugin PCL to capture photo using the device camera.

I realize that, for every photo, it creates a file in /storage/emulated/0/Android/data/com.myapp/files/Pictures

(and I can provide a subdirectory within Pictures folder and the name of the file). What I want to do is delete all these files the next time my app starts up.

Since I'm only focused on the Android version, I've set up a dependency service call to locally delete the file (which I thought was trivial) but for some reasons I simply can NOT find that file programmatically or through ADB shell.

When I plug my device in for USB file transfer on windows computer, I can see the photos I want to delete in Computer\\Moto G (4)\\Internal shared storage\\Android\\data\\com.myapp\\files\\Pictures

But I can't get to these files through code or shell. In code, I'm getting the path through

Forms.Context.GetExternalFilesDir(Android.OS.Environment.DirectoryPictures).AbsolutePath

which gives me the path

/storage/emulated/0/Android/data/com.myapp/files/Pictures

which I think is the path where my files SHOULD be. But it's come up blank. Whether I try System.IO or Java.IO , the directory comes up empty and file.delete() or System.IO.File.Delete(path) doesn't work for me.

tldr; What does

Computer\Moto G (4)\Internal shared storage\Android\data\com.myapp\files\Pictures

translate to in terms of actual physical path in Android OS which we can list from ADB Shell?

What I want to do is delete all these files the next time my app starts up.

Try using the following code, it works fine on my side :

public void DeleteFolder()
{
    var path = Forms.Context.GetExternalFilesDir(Android.OS.Environment.DirectoryPictures).AbsolutePath;

    Java.IO.File directory = new Java.IO.File(path);

    if (directory.IsDirectory)
    {
        foreach (Java.IO.File child in directory.ListFiles())
        {
            deleteRecursive(child);
        }
    }
    directory.Delete();
}

It was a false negative. I was looking at the files through Windows Explorer and for some reason, Windows kept showing me those files even after they were deleted. Obviously, the reason I couldn't se those files through ADB Shell or from within Android File Manager was because those files were never there.

Forms.Context.GetExternalFilesDir(Android.OS.Environment.DirectoryPictures).AbsolutePath

gives me

/storage/emulated/0/Android/data/com.myapp/files/Pictures

which is the same as

Computer\\Moto G (4)\\Internal shared storage\\Android\\data\\com.myapp\\files\\Pictures

Both System.IO.File.Delete(path) and new Java.IO.File(dir, children[i]).Delete(); methods work fine. It's just that windows chooses to keep showing the empty "ghost" files.

This issue has apparently been raised before to no answer: Programatically deleted files still show up in Windows Explorer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM