我想使用意图将我的云存储中的(zip)文件下载到我的应用程序。我成功地访问了我的云:
var activity = (Activity)Xamarin.Forms.Forms.Context;
// ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's file
// browser.
Intent intent = new Intent(Intent.ActionGetContent);
// Filter to only show results that can be "opened", such as a
// file (as opposed to a list of contacts or timezones)
intent.AddCategory(Intent.CategoryOpenable);
// Filter to show only images, using the image MIME data type.
// If one wanted to search for ogg vorbis files, the type would be "audio/ogg".
// To search for all documents available via installed storage providers,
// it would be "*/*".
intent.SetType("*/*");
activity.StartActivityForResult(intent, 0);
我的Mainactivity.cs中有这样的OnActivityResult:
protected override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
base.OnActivityResult(requestCode, resultCode, data);
if (requestCode == 0)
{
System.Uri uri = new System.Uri(data.Data.ToString());
WebClient myWebClient = new WebClient();
myWebClient.DownloadFile(uri, "/root"));
}
}
但我认为我的uri地址并不好,因为它是一个内容uri但不是文件uri。那么我怎么能从我的内容uri获得一个有效的文件uri?
答案 0 :(得分:0)
使用:
解决System.Uri uri = new System.Uri(data.Data.ToString());
ContentResolver rc = ContentResolver;
var stream = rc.OpenInputStream(data.Data);
try
{
using (var fileStream = System.IO.File.Create(filepath))
{
stream.CopyTo(fileStream);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("exception :" + ex.Message);
}