none
[UWP][C++] How to access files like Photos App? RRS feed

  • 问题

  • When the user in the use of the Photos App in the Explorer to open a picture file, The Photos App can access all the files in the folder where the file is located. How does it do this? How do I write code to implement this feature?

    For example, There is a test folder, It contains the following files:

    test/a.png
    test/b.png
    test/c.png
    test/d.png

    Use Photos App open test/c.png file, It can get all files in the test folder.


    • 已编辑 lc-soft 2017年3月1日 14:01 update
    2017年3月1日 10:16

答案

全部回复

  • Hi lc-soft,

    I think system photo app is different from ours what we designed.

    In UWP app, we can only access the folder we have the permission if we don’t use a Folder Picker. You can get more in file access permissions and Skip the path: stick to the StorageFile.

    If you want to get all the file in the location that app has permissions for, such as PictureLibrary, you can use the following code.

    void App1::MainPage::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    {
           FileOpenPicker^ openPicker = ref new FileOpenPicker();
           openPicker->ViewMode = PickerViewMode::Thumbnail;
           openPicker->SuggestedStartLocation = PickerLocationId::PicturesLibrary;
           openPicker->FileTypeFilter->Append(".png");
           create_task(openPicker->PickSingleFileAsync()).then([this](StorageFile^ file)
           {
                  if (file)
                  {
                         //TO DO operate the file 
    
                         //loader other the same type  file
                         auto fileTypeFilter = ref new Vector<String^>();
                         fileTypeFilter->Append(".png");
    
                         // Create query options.
                         auto queryOptions = ref new QueryOptions(CommonFileQuery::OrderByName, fileTypeFilter);
                         create_task(KnownFolders::GetFolderForUserAsync(nullptr /* current user */, KnownFolderId::PicturesLibrary)).then([this, queryOptions](StorageFolder^ picturesFolder)
                         {
                               auto query = picturesFolder->CreateFileQueryWithOptions(queryOptions);
                               return query->GetFilesAsync();
                         }).then([=](IVectorView<StorageFile^>^ files)
                         {
                               std::for_each(begin(files), end(files), [=](StorageFile^ file)
                               {
                                      // Create UI elements for the output and fill in the contents when they are available.
                                      LogMessage(file->Name);
                               });
                         });
                  }
                  else
                  {
                         //fail to get file 
                  }
           });
    }
    

    But if you want to load picture on the folder that your app don’t have a permission, you should use a FolderPicker but it can’t implement the effect you described. As the following code.

    void App1::MainPage::Button_Click1(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    {
           FolderPicker^ folderPicker = ref new FolderPicker();
           folderPicker->SuggestedStartLocation = PickerLocationId::PicturesLibrary;
    
           // Users expect to have a filtered view of their folders depending on the scenario.
           // For example, when choosing a documents folder, restrict the filetypes to documents for your application.
           folderPicker->FileTypeFilter->Append("*");
    
           create_task(folderPicker->PickSingleFolderAsync()).then([this](StorageFolder^ folder)
           {
                  if (folder)
                  {
                         //loader other the same type  file
                         auto fileTypeFilter = ref new Vector<String^>();
                         fileTypeFilter->Append(".png");
    
                         // Create query options.
                         auto queryOptions = ref new QueryOptions(CommonFileQuery::OrderByName, fileTypeFilter);
                         auto query = folder->CreateFileQueryWithOptions(queryOptions);
                         create_task(query->GetFilesAsync()).then([this](IVectorView<StorageFile^>^ files) {
    
                               std::for_each(begin(files), end(files), [=](StorageFile^ file)
                               {
                                      // Create UI elements for the output and fill in the contents when they are available.
                                      LogMessage(file->Name);
                               });
                         });
                  }
                  else
                  {
                  }
           });
    }
    

    Best regards,

    Breeze Liu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年3月2日 10:24
    版主
  • I found this document: https://docs.microsoft.com/en-us/windows/uwp/launch-resume/handle-file-activation

    This document can solve my main problem, But I do not know if it can access all the files in the folder where the file is located.

    2017年3月3日 2:25
  • Hi lc-soft,

    >>But I do not know if it can access all the files in the folder where the file is located.

    As I mentioned above, if you use a Folder Picker, you can get the all file in the folder that the user selected(see Open files and folders with a picker). The sample code is as the second part above. But if not, an app can access additional files and folders by declaring capabilities in the app manifest (see App capability declarations).Such as Documents, Music, Pictures and so on, you can get more details in file access permissions and Skip the path: stick to the StorageFile.

    Best regards,

    Breeze Liu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年3月3日 5:36
    版主
  • Hi,

    You can use the interface "IFileActivatedEventArgsWithNeighboringFiles".

    https://docs.microsoft.com/en-us/uwp/api/Windows.ApplicationModel.Activation.IFileActivatedEventArgsWithNeighboringFiles

    When user open the file from explorer and it cause the launch of your app, yes, the method OnFileActivated is inovked.  The parameter FileActivatedEventArgs have a member "NeighboringFilesQuery". This member have an interface of "IFileActivatedEventArgsWithNeighboringFiles". You can call "await NeighboringFilesQuery.GetFilesAsync()" to retrieve the neighboring files.

    https://docs.microsoft.com/en-us/uwp/api/Windows.ApplicationModel.Activation.FileActivatedEventArgs#Windows_ApplicationModel_Activation_FileActivatedEventArgs_NeighboringFilesQuery

    • 已标记为答案 lc-soft 2017年3月3日 9:03
    2017年3月3日 7:17
  • @pnp0a03_

    Good job! 

    • 已编辑 lc-soft 2017年3月3日 9:07
    2017年3月3日 9:03
  • @Breeze Liu

    I have found the answer, thanks you.

    • 已编辑 lc-soft 2017年3月3日 9:06
    2017年3月3日 9:05