GetFilesAsync () returns 0

I need an application to be able to list jpg files from the Photos folder. I am stuck on reading jpg files from an SD card on a Windows phone device.

What is being done to solve the problem:

  • created function for reading files in a collection. The function was taken from a main sample called "RouteMapper"
  • ListBox for storing a list of files
  • permission to use sd and jpg files

Function:

Photos = new ObservableCollection<ExternalStorageFile>();
private async void scanExternalStorage_Run()
        {
            // Clear the collection bound to the page.
            Photos.Clear();

            // Connect to the current SD card.
            ExternalStorageDevice _sdCard = (await ExternalStorage.GetExternalStorageDevicesAsync()).FirstOrDefault();

            // If the SD card is present, add GPX files to the Routes collection.
            if (_sdCard != null)
            {
                try
                {
                    // Look for a folder on the SD card named Routes.
                    ExternalStorageFolder photosFolder = await _sdCard.GetFolderAsync("Photos");

                    // Get all files from the Routes folder.
                    IEnumerable<ExternalStorageFile> photosFiles = await photosFolder.GetFilesAsync();

                    // Add each GPX file to the Routes collection.
                    foreach (ExternalStorageFile esf in photosFiles)
                    {
                        if (esf.Path.EndsWith(".jpg"))
                        {
                            Photos.Add(esf);
                        }
                    }
                }
                catch (FileNotFoundException)
                {
                    // No Routes folder is present.
                    MessageBox.Show("The Routes folder is missing on your SD card. Add a Routes folder containing at least one .GPX file and try again.");
                }
            }
            else
            {
                // No SD card is present.
                MessageBox.Show("The SD card is mssing. Insert an SD card that has a Routes folder containing at least one .GPX file and try again.");
            }
        }

ListBox:

<ListBox
                x:Name="jpgFilesListBox"
                ItemsSource="{Binding Photos}"
                SelectionChanged="jpgFilesListBox_SelectionChanged"
                >
                <!-- Each ListBox item is bound to the name of each GPX file -->
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <TextBlock
                            Margin="0, 12, 0, 0"
                            Text="{Binding Name}"
                            Style="{StaticResource PhoneTextTitle2Style}"
                            />
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

WMAppManifest.xml:

   <Capabilities>
      <Capability Name="ID_CAP_NETWORKING" />
      <Capability Name="ID_CAP_MEDIALIB_AUDIO" />
      <Capability Name="ID_CAP_MEDIALIB_PLAYBACK" />
      <Capability Name="ID_CAP_SENSORS" />
      <Capability Name="ID_CAP_WEBBROWSERCOMPONENT" />
      <Capability Name="ID_CAP_REMOVABLE_STORAGE" />
      <Capability Name="ID_CAP_MAP" />
    </Capabilities>

<Extensions>
     <FileTypeAssociation TaskID="_default" Name="JPG" NavUriFragment="fileToken=%s">
        <SupportedFileTypes>
          <FileType ContentType="application/jpg">.jpg</FileType>
        </SupportedFileTypes>
     </FileTypeAssociation>
 </Extensions>

Debug detected photosFolder.GetFilesAsync()returns 0. returns 0

While I definitely have files in the Photos folder. enter image description here

RouteMapper works fine and returns a list of gpx files.

PS: I am using emulator 8.1

: . GetFolderAsync , "D:\Photos" 2: . Windows Phone SilverLight .

WMAppManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<Deployment xmlns="http://schemas.microsoft.com/windowsphone/2014/deployment" AppPlatformVersion="8.1">
  <DefaultLanguage xmlns="" code="en-US" />
  <App xmlns="" ProductID="{dc1cb15e-1c8d-4635-a85e-e414e89455fb}" Title="CameraGPS" RuntimeType="Silverlight" Version="1.0.0.0" Genre="apps.normal" Author="CameraGPS author" Description="Sample description" SDOptOut="false" BackUpOptOut="false" Publisher="CameraGPS" PublisherID="{4d13b339-1a9f-4a55-ba44-fe10cf0c0420}">
    <IconPath IsRelative="true" IsResource="false">Assets\ApplicationIcon.png</IconPath>
    <Capabilities>
      <Capability Name="ID_CAP_NETWORKING" />
      <Capability Name="ID_CAP_MEDIALIB_AUDIO" />
      <Capability Name="ID_CAP_MEDIALIB_PLAYBACK" />
      <Capability Name="ID_CAP_SENSORS" />
      <Capability Name="ID_CAP_WEBBROWSERCOMPONENT" />
      <Capability Name="ID_CAP_REMOVABLE_STORAGE" />
      <Capability Name="ID_CAP_MAP" />
      <Capability Name="ID_CAP_MEDIALIB_PHOTO" />
    </Capabilities>
    <Tasks>
      <DefaultTask Name="_default" NavigationPage="MainPage.xaml" ActivationPolicy="Resume" />
    </Tasks>
    <Tokens>
      <PrimaryToken TokenID="CameraGPSToken" TaskName="_default">
        <TemplateFlip>
          <SmallImageURI IsRelative="true" IsResource="false">Assets\Tiles\FlipCycleTileSmall.png</SmallImageURI>
          <Count>0</Count>
          <BackgroundImageURI IsRelative="true" IsResource="false">Assets\Tiles\FlipCycleTileMedium.png</BackgroundImageURI>
          <Title>CameraGPS</Title>
          <BackContent>
          </BackContent>
          <BackBackgroundImageURI>
          </BackBackgroundImageURI>
          <BackTitle>
          </BackTitle>
          <DeviceLockImageURI>
          </DeviceLockImageURI>
          <HasLarge>
          </HasLarge>
        </TemplateFlip>
      </PrimaryToken>
    </Tokens>
    <Extensions>
      <FileTypeAssociation TaskID="_default" Name="jpg" NavUriFragment="fileToken=%s">
        <SupportedFileTypes>
          <FileType ContentType="image/jpg">.jpg</FileType>
        </SupportedFileTypes>
      </FileTypeAssociation>
    </Extensions>
    <ScreenResolutions>
      <ScreenResolution Name="ID_RESOLUTION_WVGA" />
      <ScreenResolution Name="ID_RESOLUTION_WXGA" />
      <ScreenResolution Name="ID_RESOLUTION_HD720P" />
    </ScreenResolutions>
  </App>
</Deployment>

Package.appxmanifest:

<?xml version="1.0" encoding="utf-8"?>
<Package xmlns="http://schemas.microsoft.com/appx/2010/manifest" xmlns:m2="http://schemas.microsoft.com/appx/2013/manifest" xmlns:m3="http://schemas.microsoft.com/appx/2014/manifest" xmlns:mp="http://schemas.microsoft.com/appx/2014/phone/manifest">
  <Identity Name="5b897434-732a-4f41-a110-db75375561c9" Publisher="CN=Qet" Version="1.0.0.0" />
  <mp:PhoneIdentity PhoneProductId="dc1cb15e-1c8d-4635-a85e-e414e89455fb" PhonePublisherId="4d13b339-1a9f-4a55-ba44-fe10cf0c0420" />
  <Properties>
    <DisplayName>CameraGPS</DisplayName>
    <PublisherDisplayName>Qet</PublisherDisplayName>
    <Logo>Assets\StoreLogo.png</Logo>
  </Properties>
  <Prerequisites>
    <OSMinVersion>6.3.1</OSMinVersion>
    <OSMaxVersionTested>6.3.1</OSMaxVersionTested>
  </Prerequisites>
  <Resources>
    <Resource Language="x-generate" />
  </Resources>
  <Applications>
    <Application Id="App" Executable="AGHost.exe" EntryPoint="MainPage.xaml">
      <m3:VisualElements DisplayName="CameraGPS" Square150x150Logo="Assets\SquareTile150x150.png" Square44x44Logo="Assets\Logo.png" Description="CameraGPS" ForegroundText="light" BackgroundColor="#464646">
        <m3:DefaultTile Square71x71Logo="Assets\SquareTile71x71.png">
        </m3:DefaultTile>
        <m3:SplashScreen Image="Assets\SplashScreen.png" />
      </m3:VisualElements>
      <Extensions>
        <Extension Category="windows.fileTypeAssociation">
          <FileTypeAssociation Name="jpg">
            <DisplayName>Jpg</DisplayName>

            <SupportedFileTypes>

              <FileType ContentType="image/jpg">.jpg</FileType>
            </SupportedFileTypes>
          </FileTypeAssociation>
        </Extension>
        <Extension Category="windows.accountPictureProvider" />
        <Extension Category="windows.backgroundTasks" EntryPoint="AgHost.BackgroundTask">
          <BackgroundTasks>
            <Task Type="systemEvent" />
            <m2:Task Type="location" />
          </BackgroundTasks>
        </Extension>
      </Extensions>
    </Application>
  </Applications>
  <Capabilities>
    <Capability Name="removableStorage" />
    <Capability Name="picturesLibrary" />
    <DeviceCapability Name="location" />
    <DeviceCapability Name="webcam" />
  </Capabilities>
  <Extensions>
    <Extension Category="windows.activatableClass.inProcessServer">
      <InProcessServer>
        <Path>AgHostSvcs.dll</Path>
        <ActivatableClass ActivatableClassId="AgHost.BackgroundTask" ThreadingModel="both" />
      </InProcessServer>
    </Extension>
  </Extensions>
</Package>
+4

Source: https://habr.com/ru/post/1673516/


All Articles