Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FileopenPicker (UWP) throws error when PickSingleFileAsync() called #23

Closed
jasells opened this issue Aug 29, 2018 · 8 comments
Closed
Assignees

Comments

@jasells
Copy link
Contributor

jasells commented Aug 29, 2018

"WinRT information: The FileTypeFilters property must have at least one file type filter specified."

There is no way to set the filter... it is read only, and not virtual.

FileOpenPicker.cs

Am I missing some other initialization step?

@jamesmcroft
Copy link
Member

jamesmcroft commented Aug 29, 2018

You can add file type filters like so:

var singleFilePick = new FileOpenPicker();
            singleFilePick.FileTypeFilter.Add(".jpg");

var pickedFile = await singleFilePick.PickSingleFileAsync();

Let me know if this solves your issue.

@jasells
Copy link
Contributor Author

jasells commented Aug 29, 2018

Duh, I’m being dense… thank you. End of a long day...

@jasells jasells closed this as completed Aug 29, 2018
@jasells
Copy link
Contributor Author

jasells commented Aug 30, 2018

James,
maybe you have some more insight into a problem I'm having now... have you used this code on UWP, or only on Droid/iOS?

I am trying to open a file on UWP, but keep getting error stating file does not exists. I think the source is line#62 here: StorageFile.cs

I would expect SystemIO.File.Exists() to work on Dorid/iOS, but not UWP (always returns false due to difference in sec vs. desktop API on windows).

I am interested in using several of your libs with Xamarin.Forms to support multiplatform, but I suspect you have not designed for use on UWP.

@jasells
Copy link
Contributor Author

jasells commented Aug 30, 2018

I have proven that the issue is with System.IO.File.Exists() on UWP. It no work like expected =( . Sad that .NetStandard provides an API that doesn't behave... but that's why we have the PCL mess we have … anyway, that's a different topic.

If you were to replace line #62 in StorageFile.cs
public bool Exists => this.Originator != null && File.Exists(this.Path);

with something like:
public bool Exists => this.Originator != null && CheckFileExists(this.Path);
.
.
.
protected virtual bool CheckFileExists(string path) => File.Exists(path)

I could then provide a different impl for UWP. Is supect there is similar code in StorageFolder.cs (line 65)

Edit: I just saw that the StorageFile class is sealed.... makes a solution harder.

@jamesmcroft jamesmcroft reopened this Aug 31, 2018
@jamesmcroft
Copy link
Member

@jasells I will take a look into this. It has previously been working but as you've mentioned, changes in .NET Standard may have broken this and as you've pointed out, this is because File.Exists doesn't work if the file in UWP is outside of the scope of the application's storage location.

@jamesmcroft jamesmcroft self-assigned this Aug 31, 2018
@jasells
Copy link
Contributor Author

jasells commented Aug 31, 2018

The only way I've been able to accomplish the File.Exists fuctionality (even in a pure UWP app) is to get the file's parent folder object, and the call GetItemAsync() and check the result for null.

I did this as a work-around for now in my Xamarin code:

`public async Task<System.IO.Stream> OpenFileForRead(XPlat.Storage.IStorageFile file)
{
var dir = await file.GetParentAsync();
Windows.Storage.IStorageFile nativeFile;
Windows.Storage.IStorageFolder parentDir = null;

        if (dir.Path.ToLower().Contains("c:"))
        {
           //check all the known folders
        }
        else //it is removable drive
        {
            var fldrs = (await Windows.Storage.KnownFolders.RemovableDevices.GetFoldersAsync()).ToList();

            parentDir = fldrs.FirstOrDefault(fldr => fldr.Name == dir.Name);

            var name = parentDir.Name;
        }

        nativeFile = (await parentDir.GetItemAsync(file.Name)) as Windows.Storage.IStorageFile;

        return (await nativeFile.OpenReadAsync()).AsStream();
    }`

This is basically what I did using Xamarin's Dependency service to provide the UWP plugin code above, and then I used an extension method in my portable code to add it back to Xplat.Storage.IStorageFile and just not using OpenReadAsync() provided.

It might be more a function of the target/min UWP version, not .NetStandard, if you say it has been working? I am targeting min of 1709 (SDK v. 16299).

@jasells
Copy link
Contributor Author

jasells commented Sep 5, 2018

Have you looked into this yet? I have found several similar issues. It seems that the System.IO.File.Exists API is not behaving as expected... I am basically having to work around anything that checks for the existance of the file, some of them could be avoided as I think they are unnecessary checks. But I get why you did it if .Exists worked previously. The native errors are a little vague sometimes. At this point though, I am going to have to re-impl almost everything to make it work on UWP.

If you are not working on them yet, I may pull a fork and start playing with it. I would like to build onto what you have with a XPlat.Storage.Portable namespace that has Xamarin plugin impl built in. I think it would be a separate package that depends on yours, so as to not force Xamarin dependencies on anything currently using these libs. Open to suggestions there.

@jasells
Copy link
Contributor Author

jasells commented Sep 6, 2018

I think the appropriate thing to do here is close this issue, and open a new one with a more appropriate title/focus.

@jasells jasells closed this as completed Sep 6, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants