【发布时间】:2018-06-22 06:22:11
【问题描述】:
我正在尝试跟踪图片库的更改,因为我希望我的应用程序将新照片上传到服务器。为了跟踪更改,我在这里关注了 MSDN 文章 https://msdn.microsoft.com/en-us/magazine/mt790201.aspx
如果我在手机上运行我的代码(带有 Fall Creators Update 的 Windows 10 移动版),如果将图片保存到 sd 卡,它将无法工作。但是,如果删除 sd 卡并重新启动我的手机,我可以从更改跟踪器中读取更改。在台式电脑上一切正常。
这就是我为更改跟踪器启用后台任务的方式:
public async Task Register()
{
// Check if your app has access to the background
var requestStatus = await BackgroundExecutionManager.RequestAccessAsync();
if (!(requestStatus ==
BackgroundAccessStatus.AllowedMayUseActiveRealTimeConnectivity ||
requestStatus == BackgroundAccessStatus.AllowedSubjectToSystemPolicy ||
requestStatus ==
BackgroundAccessStatus.AllowedWithAlwaysOnRealTimeConnectivity ||
requestStatus == BackgroundAccessStatus.AlwaysAllowed))
{
Debug.WriteLine("Failed to get access to the background");
return;
}
// Build up the trigger to fire when something changes in the pictures library
var builder = new BackgroundTaskBuilder();
builder.Name = "Photo Change Trigger";
StorageLibrary picturesLib =
await StorageLibrary.GetLibraryAsync(KnownLibraryId.Pictures);
var picturesTrigger = StorageLibraryContentChangedTrigger.Create(picturesLib);
// We are registering to be activated in OnBackgroundActivated instead of
// BackgroundTask.Run; either works, but I prefer the single-process model
builder.SetTrigger(picturesTrigger);
BackgroundTaskRegistration task = builder.Register();
}
这就是我获得更改的方式,其中包含不起作用的代码:
public async Task GetChanges()
{
StorageLibrary picturesLib =
await StorageLibrary.GetLibraryAsync(KnownLibraryId.Pictures);
StorageLibraryChangeTracker picturesTracker = picturesLib.ChangeTracker;
picturesTracker.Enable();
StorageLibraryChangeReader changeReader = picturesTracker.GetChangeReader();
// if photos are saved on the SD-card the next line does not work
IReadOnlyList<StorageLibraryChange> changes = await changeReader.ReadBatchAsync();
}
是我做错了什么还是这是 Windows 10 移动版中的错误?我已经尝试将我的设备恢复出厂设置或重新格式化我的 SD 卡,但没有任何效果。
【问题讨论】:
-
在手机上运行代码并将图片保存到SD卡时,能否告诉我们是否遇到任何错误或返回null?根据这篇文章:msdn.microsoft.com/en-us/magazine/mt790201.aspx,它表明由于 SD 卡可以在设备关闭时从设备中移除,并且基于 FAT 的文件系统上没有日志,因此无法保证在引导期间在 SD 卡上进行更改跟踪会议。
-
我得到一个异常:HRESULT 异常:0x80080222 是的,图片已保存到 SD 卡,设备一直处于开机状态,无需重启。
标签: c# uwp windows-10-universal windows-10-mobile change-tracking