WP8.1:onedrive操作
小梦今天给大家分享一下windows phone 8.1开发 onedrive中的一些操作:
- Windows phone 8.1 中 onedrive 登录
- Windows phone 8.1 中 onedrive 文件/照片上传,更新
- Windows phone 8.1 中 onedrive文件/照片下载
- Windows phone 8.1 中 onedrive文件夹建立,删除,移动,复制
- Windows phone 8.1 中 onedrive的一些关键概念
最后有源代码。
Windows phone 8.1的Live SDK下载:
进入 http://msdn.microsoft.com/onedrive/dn630256 页面 选择 适用于 Windows、Windows Phone 和 .NET 的 Live SDK 下载 ,安装即可。安装后再引用 Live SDK 。别急,还没完。
将应用程序和应用商店关联:
在工具栏 应用商店中选择 将应用程序和应用商店关联 。按着提示完成即可。好了,下来就然我们正式开始吧。
Windows phone 8.1 中 onedrive 登录:
好了,先上代码,我们在慢慢解释。
LiveLoginResult result;
private async void loginButton_Click(object sender, RoutedEventArgs e)//登录
{
try
{
msgText.Text = "亲:正在登录"; var authClient = new LiveAuthClient();//初始化,用来获取用户数据。
result = await authClient.LoginAsync(new string[] { "wl.signin", "wl.skydrive", "wl.skydrive_update", "wl.photos" });
//登录,里面的参数是 Scopes
if (result.Status == LiveConnectSessionStatus.Connected)
{ var connectClient = new LiveConnectClient(result.Session);
var meResult = await connectClient.GetAsync("me");
msgText.Text = "亲爱的:" + meResult.Result["name"].ToString() + " 您已成功登录OneDrive!";
} }
catch (LiveAuthException ex)
{
msgText.Text = ex.Message;
}
catch (LiveConnectException ex)
{
msgText.Text = ex.Message;
}
}
Scopes:
Scopes 翻译为域 。意思是授权允许应用获取用户的哪些数据。 域分为核心域和扩展域: 核心域包括: wl.basic :获取用户的基本信息,也可以获得用户的联系人。 wl.offline_access:可以任何时候读取和更新用户数据。 wl.signin:单点登录。即只要用户已经登录,只需用户授权即可,不需再次登录。在windows phone 上推荐使用。 扩展域: 扩展域很多,常用的主要有: wl.photos:授权获取用户的图片,视频,唱片集,音频等。 wl.skydrive:读取用户在onedrive 上存储的文件 wl.skydrive_update:上传,修改用户onedrive 上存储的文件
用户基本信息的json数据格式:
{ "id": "8c8ce076ca27823f", "name": "Roberto Tamburello", "first_name": "Roberto", "last_name": "Tamburello", "gender": null, "locale": "en_US" } 也就是上例的meResult.Result。
Windows phone 8.1 中 onedrive 上传文件:
private async void uploadButton_Click(object sender, RoutedEventArgs e)//上传
{
try
{
msgText.Text = "亲:正在上传";
if (result.Session != null)
{
var liveConnectClient = new LiveConnectClient(result.Session);
//读取文件
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
StorageFile file = await localFolder.GetFileAsync(App.TestFileName); LiveUploadOperation uploadOperation = await liveConnectClient.CreateBackgroundUploadAsync(
"me/skydrive", file.Name, file, OverwriteOption.Rename);
LiveOperationResult uploadResult = await uploadOperation.StartAsync(); }
}
catch (LiveAuthException ex)
{
msgText.Text = ex.Message;
}
catch (LiveConnectException ex)
{
msgText.Text = ex.Message;
} }
Windows phone 8.1 中 onedrive 下载文件:
我们已知要下载的文件名为:Test.txt。也就是刚上传的文件。
private async void downButton_Click_1(object sender, RoutedEventArgs e)//下载
{
try
{
msgText.Text = "亲:正在下载";
string id = string.Empty; if (result.Session != null)
{
var connectClient = new LiveConnectClient(result.Session); LiveOperationResult operationResult = await connectClient.GetAsync("me/skydrive/search?q=Test.txt");
List<object> items = operationResult.Result["data"] as List<object>;
IDictionary<string, object> file = items.First() as IDictionary<string, object>;
id = file["id"].ToString(); LiveDownloadOperation operation = await connectClient.CreateBackgroundDownloadAsync(string.Format("{0}/content", id));
var results = await operation.StartAsync();
string strings=results.Stream.ToString(); StorageFolder localFolder = ApplicationData.Current.LocalFolder;
StorageFile files = await localFolder.GetFileAsync(App.TestFileName); await FileIO.WriteTextAsync(files, strings); msgText.Text = "恭喜:您已成功从OneDrive下载文件!"; }
}
catch (Exception ex)
{
msgText.Text = ex.Message;
}
}
关于请求参数的说明:
filter:指定项目类型(all(默认类型)、photos、videos、audio、folders 或 albums)来仅获取某些类型的项目。例如,若要仅获取照片,请使用 FOLDER_ID/files?filter=photos。 limit:指定要获取的项目数来获取有限数量的项目。例如,若要获取前两个项目,请使用 FOLDER_ID/files?limit=2。 offset: offset 参数设置为要获取的第一个项目的索引来指定要获取的第一个项目。例如,若要获取从第三个项目开始的两个项目,请使用 FOLDER_ID/files?limit=2&offset=3。 q:获取指定名称的文件。例如上例中的:me/skydrive/search?q=Test.txt
Windows phone 8.1 中 onedrive 上传图片:
上传和图片和文件是一样的方法:
private async void uploadPiactrue_Click(object sender, RoutedEventArgs e)
{ StorageFile file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync("2.jpg");
try
{
msgText.Text = "亲:正在上传"; if (result.Session != null)
{
var liveConnectClient = new LiveConnectClient(result.Session); LiveUploadOperation uploadOperation = await liveConnectClient.CreateBackgroundUploadAsync(
"me/skydrive/camera_roll", file.Name, file, OverwriteOption.Overwrite);
LiveOperationResult uploadResult = await uploadOperation.StartAsync(); if (uploadResult.Result != null)
{
msgText.Text = "恭喜:您已成功上传OneDrive!";
} }
}
catch (LiveAuthException ex)
{
msgText.Text = ex.Message;
}
catch (LiveConnectException ex)
{
msgText.Text = ex.Message;
}
}
onedrive使用友好名称访问特定文件夹:
要访问特定的 OneDrive 文件夹,你可以使用友好名称,而非文件夹 ID。你可以在 OneDrive UI 中使用以下友好名称来访问相应的文件夹: •USER_ID/skydrive/camera_roll 表示“OneDrive 本机照片”文件夹。 •USER_ID/skydrive/my_documents 表示“文档”文件夹。 •USER_ID/skydrive/my_photos 表示“图片”文件夹。 •USER_ID/skydrive/public_documents 表示“公用”文件夹。 一般情况下,将 USER_ID 替换为 me(对于已登录用户) 。如上例,就是将图片上传至本机照片文件夹。
Windows phone 8.1 中 onedrive 下载图片:
private async void downPictrue_Click(object sender, RoutedEventArgs e)
{
msgText.Text = "亲:正在下载";
string idPictrue = string.Empty;
string namePictrue = string.Empty;
try
{
LiveConnectClient liveClient = new LiveConnectClient(result.Session);
LiveOperationResult operationResult =
await liveClient.GetAsync("me/skydrive/camera_roll");
dynamic results = operationResult.Result;
string idfolder= results.id;
LiveOperationResult pictrueResult = await liveClient.GetAsync(idfolder+"/photos");
dynamic pictrues = pictrueResult.Result;
foreach (dynamic pictrue in pictrues.data)
{
namePictrue = pictrue.name;
idPictrue = pictrue.id;
StorageFile pictrueFile = await ApplicationData.Current.LocalFolder.CreateFileAsync(namePictrue,CreationCollisionOption.ReplaceExisting);
await liveClient.BackgroundDownloadAsync(idPictrue+"/picture?type=thumbnail", pictrueFile);
var imgSource = new BitmapImage();
var stream = await pictrueFile.OpenReadAsync();
imgSource.SetSource(stream);
imgResult.Source = imgSource; } msgText.Text = "恭喜:您已成功从OneDrive下载图片!";
} catch (LiveConnectException exception)
{
msgText.Text = "Error getting album info: " + exception.Message;
}
onedrive项目的预览:
若要显示 OneDrive 项目的预览,请向 /skydrive/get_item_preview?type=TYPE&url=URL 发出 GET 请求。可选的 type 查询字符串参数为以下值之一: •thumbnail (缩略图) •small(为了获得最大值 100 × 100 像素预览) •album(最大 200 × 200) •normal(最大 800 × 800)
Windows phone 8.1 中 onedrive 建立文件夹:
private async void creatFodel_Click(object sender, RoutedEventArgs e)
{ try
{
if (result.Session != null)
{
var folderData = new Dictionary<string, object>();
folderData.Add("name", "Test");
LiveConnectClient liveClient = new LiveConnectClient(result.Session);
LiveOperationResult operationResult =
await liveClient.PostAsync("me/skydrive", folderData); msgText.Text = string.Join(" ", "新建文件夹", operationResult.Result["name"].ToString(),
"ID:", operationResult.Result["id"].ToString());
} }
catch (LiveConnectException exception)
{
msgText.Text = "Error creating folder: " + exception.Message;
} }
Windows phone 8.1 中 onedrive 删除文件夹:
private async void deleteFodel_Click(object sender, RoutedEventArgs e)
{
string id = string.Empty;
LiveConnectClient liveClient = new LiveConnectClient(result.Session);
LiveOperationResult operationResult = await liveClient.GetAsync("me/skydrive/search?q=Test");
List<object> items = operationResult.Result["data"] as List<object>;
IDictionary<string, object> file = items.First() as IDictionary<string, object>;
id = file["id"].ToString();
LiveOperationResult operationResult1 =
await liveClient.DeleteAsync(id);
msgText.Text = "删除文件夹成功";
}
Windows phone 8.1 中 onedrive 移动,复制文件夹:
LiveOperationResult operationResult =
await moveFileClient.MoveAsync("file.9d24497f7fef8f33.9D24497F7FEF8F33!748",
"folder.9d24497f7fef8f33.9D24497F7FEF8F33!265");
第一参数是你要移动的文件ID,第二个参数是你要移动的文件的ID。复制的话将MoveAsync改为CopeAsync,然后俩个参数改为你想要复制前后的文件或者文件夹的ID即可。 LiveConnectClient.GetAsync在返回的 JavaScript 对象表示法 (JSON) 格式的对象中,要务必注意两个结构:id 结构,表示顶层文件夹的文件夹 ID; 如果向 me/skydrive/files 发出请求,则返回的 JSON 格式的对象仅包含有关用户顶层文件夹中所有子文件夹、子唱片集和文件的信息 要获取用户近期最常用的 OneDrive 文档列表,请使用 wl.skydrive 作用域向 /USER_ID/skydrive/recent_docs 发出 GET 请求
获取用户的 OneDrive 总存储配额和剩余存储配额
要获取有关用户在 OneDrive 中可用和未用存储空间的信息,请向 /me/skydrive/quota 或 /USER_ID/skydrive/quota 进行 GET 调用 FOLDER_ID/files,其中 FOLDER_ID 代表有效的文件夹 ID,可以获取该文件夹中所有项目的列表 通过使用上面代码中的 sort_by 参数指定如下条件来设置项目的排序条件:updated、name、size 或 default。例如,若要按名称对项目进行排序,请使用 FOLDER_ID/files?sort_by=name。 通过使用上面代码中的 sort_order 参数指定如下顺序来设置项目的排序顺序:ascending 或 descending。例如,若要按降序对项目进行排序,请使用 FOLDER_ID/files?sort_order=descending。
Windows phone 8.1 中 onedrive 唱片集:
•me/albums,可以获取有关已登录用户的所有唱片集的信息。 •/me/skydrive/shared/albums,可以获取有关已登录用户的所有共享唱片集的信息。 •USER_ID/albums,可以获取有关用户的与有效 USER_ID 相对应的所有唱片集的信息。 •使用 ALBUM_ID/photos 或 ALBUM_ID/videos 获取唱片集中所有照片或视频的列表。之后,你的代码可以进行另一个调用,该调用使用特定的照片 ID 或视频 ID 获取相应照片或视频的信息。 •使用 ALBUM_ID/files 获取唱片集中所有照片、视频、音频、子唱片集和子文件夹的列表。之后,你的代码可以进行另一个调用,该调用使用特定的照片 ID、视频 ID、唱片集 ID、音频 ID 或文件夹 ID 获取相应照片、视频、唱片集、音频或文件夹的信息。 •将上面代码中带有可选 type 参数的 /picture 与下列值之一结合使用来获取照片或视频的图片:small、normal(在未指定 type 参数时为默认值)、album、thumbnail 或 full。例如,若要获取照片的缩略图,请使用 PHOTO_ID/picture?type=thumbnail。 •通过使用 VIDEO_ID/video 获取视频的内容。
Windows phone 8.1 中 onedrive 遍历文件:
遍历onedrive中所有文件并获取指定文件。
await connectClient.GetAsync(“me/skydrive/files”);
List<object> items = operationResult.Result["data"] as List<object>;
foreach (object item in items) {
IDictionary<string, object> file = item as IDictionary<string, object>;
if (file["name"].ToString() == “WorkBook.xml”)
{ id = file["id"].ToString(); }
}
Windows phone 8.1 中 onedrive操作源代码:
http://www.bcmeng.com/bbs/forum.php?mod=viewthread&tid=188&extra=page%3D1 希望能大家通过网站的广告支持下小梦,谢谢!
WP8.1:onedrive操作的更多相关文章
- windows phone 8.1开发 onedrive操作详解
原文出自:http://www.bcmeng.com/onedrive/ 小梦今天给大家分享一下windows phone 8.1开发 onedrive中的一些操作: Windows phone 8. ...
- 与众不同 windows phone (37) - 8.0 文件系统: StorageFolder, StorageFile, 通过 Uri 引用文件, 获取 SD 卡中的文件
[源码下载] 与众不同 windows phone (37) - 8.0 文件系统: StorageFolder, StorageFile, 通过 Uri 引用文件, 获取 SD 卡中的文件 作者:w ...
- WP8.1中C++的winodws运行时组件位移操作的差异
最近学习WP8.1应用开发,想把C语言的SM3国密算法移植到手机app中.由于把C语言的代码转换成C#代码工作量较大,因此只能用winodws运行时组件来实现. SM3国密算法是一种HASH算法,具体 ...
- WP8.1 Study12:文件压缩与Known Folder(包含SD卡操作)
一.文件压缩 当应用程序保存和加载数据,它可以使用压缩. 1.使用 Windows.Storage.Compression.Compressor 压缩,获得一个Compressor stream. v ...
- wp8.1 app退出操作提示
微软的wp8.1 sdk相比之前wp8 sdk以及相关dll类库,微软又重新编译过,相关系统类库也经过精简,删改了部分传统dll库中的方法对象,很多常用方法对象被写进Windows.UI为前缀的命名空 ...
- Cocos2d-x项目移植到WP8系列之四:文件操作
原文链接: http://www.cnblogs.com/zouzf/p/3972457.html 读写文件Cocos已经用fopen fwrite来做好了,这里说的主要是文件和文件夹的创建.删除.判 ...
- WP8.1&UWP手机设备对状态栏操作
改UWP和WP8.1手机设备的状态栏.首先先说较为普遍的WP8.1设备:首先添加引用:using Windows.UI.ViewManagement;其次就可以使用StatusBar了,它提供了以下方 ...
- WP8触摸感应Manipulation的操作
触控感应不同事件的处理: 可将以下三个事件,绑定到一个控件中. /// <summary> /// 触摸开始事件 /// </summary> /// <param na ...
- WP8.1开发中关于媒体(图片)文件的生成操作,属性如何设置(内容/嵌入资源等);
(转载)WindowsPhone问题笔记-- 正确选择build action 解决媒体资源无法读取问题 链接:http://www.cnblogs.com/qinxg/archive/2012/07 ...
随机推荐
- Codefroces 784 愚人节题目(部分)
A. Numbers Joke time limit per test 2 seconds memory limit per test 64 megabytes input standard inpu ...
- Spider_lxml
xpath工具(解析) xpath 在XML文档中查找信息的语言,同样适用于HTML文档的检索 xpath辅助工具 Chrome插件 :XPath Helper 打开 :Ctrl + Shift + ...
- Eclipse导入Maven项目出现错误:Unsupported IClasspathEntry kind=4
使用Eclipse导入Maven项目失败,提示: An internal error occurred during: "Importing Maven projects". Un ...
- 解决XCODE配置LLVM环境出现的问题
问题描写叙述:在LLVM整合进XCODE的过程中出现符号没有定义导致出现未决函数或方法.但使用终端编译链接生成程序没有问题. 问题产生原因:未引用响应的LLVM库与系统库,以及编译器设置.连接器设置不 ...
- wechat4j框架具体解释
发送消息: 基于上面access_token的逻辑,在构造发送消息对象的时候请依照例如以下代码. wechat4j和微信强力推荐的方法 CustomerMsg customerMsg = new Cu ...
- eclipse-ADT安装失败经验
今天下载了一个eclipse,结果ADT死活安装不成功,网上试了很多的方法,最后还是失败了.最后听从同事的建议,直接使用adt-bundle了.这个环境基本上都是配置好的. 下载地址 http://w ...
- @Import注解
转自:https://blog.csdn.net/heyutao007/article/details/74994161 @Import注解就是之前xml配置中的import标签,可以用于依赖第三方包 ...
- 1.3 Quick Start中 Step 1: Download the code官网剖析(博主推荐)
不多说,直接上干货! 一切来源于官网 http://kafka.apache.org/documentation/ 不要局限于,这个版本,我只是以最新的版本,来做个引子,让大家对官网的各个kafka版 ...
- 动态规划例子:Maximal Square
Given a 2D binary matrix filled with 0's and 1's, find the largest square containing all 1's and ret ...
- 将一个字符串当做一个方法名或对象的key
var func = "test" // 方法 [func](){ console.log("test===>") } //调用 test() //打印 ...