Reputation: 331
I am stuck trying to implement a feature for an UWP application that I am making. I have a MediaElement declared in my XAML doc like this:
<MediaElement x:Name="_media" AutoPlay="True" IsLooping="True" Margin="0,0,8,227" AreTransportControlsEnabled="true" RenderTransformOrigin="0.499,0.41" >
<MediaElement.RenderTransform>
<CompositeTransform ScaleX="1"/>
</MediaElement.RenderTransform>
</MediaElement>
I need to be able to load videos from anywhere in my computer but UWP cannot access all directories. I am loading video files through a button (code below). Long story short, I try to copy the file into the LocalAppData folder because I know I have permission to access files there.
First Question: Can I copy files via CopyFileA() the way that I'm using it at the code below?
Second Question After copying the file, I need to add it to my application's Assets. How can I do this via code?
Here is the code that I run when I try to load a file.
void SDKTemplate::Scenario4_ReproVideo::Button_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
//This lines are here to get the path of the local folder of my app
Windows::Storage::StorageFolder^ f = Windows::Storage::ApplicationData::Current->LocalFolder;
stdext::cvt::wstring_convert<std::codecvt_utf8<wchar_t>> convert_local_path;
Platform::String^ path_ss = f->Path;
txtBlockOutput->Text = path_ss;
std::string path_local = convert_local_path.to_bytes(path_ss->Data());
FileOpenPicker^ fop = ref new FileOpenPicker();
fop->SuggestedStartLocation = PickerLocationId::Desktop;
fop->FileTypeFilter->Append(".mp4");
fop->FileTypeFilter->Append(".wmv");
create_task(fop->PickSingleFileAsync()).then([this](StorageFile^ file) {
if (file)
{
//We take the path of the file
Platform::String^ path = file->Path;
stdext::cvt::wstring_convert<std::codecvt_utf8<wchar_t>> convert_path;
std::string sourcePath = convert_path.to_bytes(path->Data());
//We take the name of the file
Platform::String^ name = file->Name;
stdext::cvt::wstring_convert<std::codecvt_utf8<wchar_t>> convert_name;
std::string name_s = convert_name.to_bytes(name->Data());
//We find out the local path (again) not sure if needed
Platform::String^ path_ss = Windows::Storage::ApplicationData::Current->LocalFolder->Path;
stdext::cvt::wstring_convert<std::codecvt_utf8<wchar_t>> convert_local_path;
std::string path_local = convert_local_path.to_bytes(path_ss->Data());
// Use Path class to manipulate file and directory paths.
std::string sourceFile = sourcePath +"/" + name_s;
std::string destFile = path_local +"/"+ name_s;
if (CopyFileA(sourceFile.c_str(), destFile.c_str(), FALSE)) {
txtBlockOutput->Text = "File is copied into local directory";
}
else { txtBlockOutput->Text = "File didn't copy"; }
_media->Source = ref new Uri(destFile);
}
else
{
txtBlockOutput->Text = "Operation cancelled.";
}
});
Upvotes: 0
Views: 391
Reputation: 3808
With windows 10 version 1803, SDK build 17134, you can access all files that the user has access to using the broadFileSystemAccess
capability.
On the other hand, your app has the permission to access the local LocalAppData location, you can access the file dirctly using the ms-appdata
uri,
using Windows::Storage;
auto getFileTask = create_task(StorageFile::GetFileFromApplicationUriAsync(ref new Uri("ms-appdata:///local/file.txt")));
getFileTask.then([](StorageFile^ file)
{
// Process file
});
So you don't need to copy the file to your application's Assets folder. Moreover, the application Assets folder is read only and is not writeable once deployed. you can not make any change in your app code including creating or copying file to this folder.
You should look into the document File access permissions to get more details about file access, and get the reference code from official FileAccess code sample.
---Update to add the broadFileSystemAccess
capability ---
Firstly, right click the Package.appxmanifest file and select View Code, you will able to see the code in the manifest.
Secondly, in the top tab, add the following code, xmlns:rescap="..."
and put the rescap
into the IgnorableNamespaces
value, looks like IgnorableNamespaces="uap mp rescap"
, it will look like,
<Package
...
xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
IgnorableNamespaces="uap mp rescap">
At last, add the broadFileSystemAccess
capability into the tab,
<Capabilities>
<rescap:Capability Name="broadFileSystemAccess" />
</Capabilities>
Upvotes: 1