英文:
Download path in Xamarin iOS
问题
我正在使用Xamarin.Forms编写代码以在iOS平台下载文件。它可以成功下载文件,没有任何错误。但是,在下载完成后,我无法在我的苹果设备上找到已下载的文件。
在调试过程中,我发现它显示的路径是:
/var/mobile/Containers/Data/Application/1234567A-B8CD-9EF9-C850-9G73587DC7C/Documents/XF_Downloads/hausmann_abcd.jpg
文件保存在哪个位置?以下是相应的图像。
我已经编写了以下代码来实现这一功能:
public class IosDownloader : IDownloader
{
public event EventHandler<DownloadEventArgs> OnFileDownloaded;
public void DownloadFile(string url, string folder)
{
string pathToNewFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), folder);
Directory.CreateDirectory(pathToNewFolder);
try
{
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
string pathToNewFile = Path.Combine(pathToNewFolder, Path.GetFileName(url));
webClient.DownloadFileAsync(new Uri(url), pathToNewFile);
}
catch (Exception ex)
{
if (OnFileDownloaded != null)
OnFileDownloaded.Invoke(this, new DownloadEventArgs(false));
}
}
private void Completed(object sender, AsyncCompletedEventArgs e)
{
if (e.Error != null)
{
if (OnFileDownloaded != null)
OnFileDownloaded.Invoke(this, new DownloadEventArgs(false));
}
else
{
if (OnFileDownloaded != null)
OnFileDownloaded.Invoke(this, new DownloadEventArgs(true));
}
}
}
<details>
<summary>英文:</summary>
I am using Xamarin.Forms and written the code to download the file for the iOS
platform. It is downloading the file successfully without any error. But after downloading it, I am not able to find the downloaded file in my apple device.
During debugging I found that it is showing
/var/mobile/Containers/Data/Application/1234567A-B8CD-9EF9-C850-9G73587DC7C/Documents/XF_Downloads/hausmann_abcd.jpg
path. So at which location file get saved? below is the image for the same.
[![enter image description here][1]][1]
[1]: https://i.stack.imgur.com/APNF6.png
I have written below code for this
public class IosDownloader : IDownloader
{
public event EventHandler<DownloadEventArgs> OnFileDownloaded;
public void DownloadFile(string url, string folder)
{
string pathToNewFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), folder);
Directory.CreateDirectory(pathToNewFolder);
try
{
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
string pathToNewFile = Path.Combine(pathToNewFolder, Path.GetFileName(url));
webClient.DownloadFileAsync(new Uri(url), pathToNewFile);
}
catch (Exception ex)
{
if (OnFileDownloaded != null)
OnFileDownloaded.Invoke(this, new DownloadEventArgs(false));
}
}
private void Completed(object sender, AsyncCompletedEventArgs e)
{
if (e.Error != null)
{
if (OnFileDownloaded != null)
OnFileDownloaded.Invoke(this, new DownloadEventArgs(false));
}
else
{
if (OnFileDownloaded != null)
OnFileDownloaded.Invoke(this, new DownloadEventArgs(true));
}
}
}
</details>
# 答案1
**得分**: 3
当文件保存在应用程序中时,它是应用程序沙盒内的临时URL。要使此图像文件通过苹果的`Photos`公开访问,您需要使用本机代码执行请求,将新的`PHImageAsset`添加到照片库中。
在表单中,您需要访问本机框架,因此必须运行本机代码。如果您不知道如何操作,网上有很多示例可供参考。但如果您想在共享代码框架中运行本机代码,这里是一个简介:https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/introduction
以下是一个示例,演示如何将临时文件URL保存到`Photos`框架中,一旦您能够使用本机的`Xamarin.iOS`框架编写代码:
```csharp
public void AddImageUrlToPhotosLibrary(string urlToSaveToPhotos)
{
PHPhotoLibrary.SharedPhotoLibrary.PerformChanges(() => {
//这是绑定到本机的https://developer.apple.com/documentation/photokit/phassetchangerequest/1624060-creationrequestforasset
//参数是图像路径的NSURL。
PHAssetChangeRequest.FromImage(new NSUrl(urlToSaveToPhotos));
}, (completed, error) => {
if (completed)
{
if (error != null)
{
Console.WriteLine($"Failed saving image asset {error.LocalizedDescription}");
} else
{
Console.WriteLine($"Successfully saved image to photos library.");
}
}
});
}
请注意,上述代码是使用C#编写的,并使用Xamarin.iOS框架与苹果的Photos框架进行交互。
英文:
When a file is saved on an application it is a temporary url within the app's sandbox. To make this image file be publicly accessible through Apple's Photos
, You'll have to use native code to do a request to add a new PHImageAsset
to the photos library.
In forms you would need to access the native frameworks and therefore run native code. There are plenty of examples of doing this online if you don't know how to already. But here is an introduction if you want to run native code within a shared code framework. https://learn.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/introduction
Here is a sample of taking that temp file URL and saving it to Photos
Framework once you can code w/ native Xamarin.iOS
frameworks:
public void AddImageUrlToPhotosLibrary(string urlToSaveToPhotos)
{
PHPhotoLibrary.SharedPhotoLibrary.PerformChanges(() => {
//This is bound to native https://developer.apple.com/documentation/photokit/phassetchangerequest/1624060-creationrequestforasset
//Parameter is an NSURL of path to image.
PHAssetChangeRequest.FromImage(new NSUrl(urlToSaveToPhotos));
}, (completed, error) => {
if (completed)
{
if (error != null)
{
Console.WriteLine($"Failed saving image asset {error.LocalizedDescription}");
} else
{
Console.WriteLine($"Successfully saved image to photos library.");
}
}
});
}
答案2
得分: 1
无法在我的苹果设备中找到已下载的文件。
如果您使用模拟器,可以通过在Mac中输入以下路径直接找到文件夹:
您的Mac
--> 选择Finder
--> 然后打开前往
菜单 --> 单击前往文件夹
,如我在此帖子中所描述的:
我在iPhone模拟器上哪里可以找到MyDocuments文件夹?
如果您在真实设备上安装了应用程序,可以按照以下线程中描述的步骤浏览文件:
Xcode
--> 设备和模拟器
--> 下载容器
,如此线程中所述:
浏览由我开发的iOS应用程序在设备上创建的文件,下载容器后,您可以右键单击它并选择显示包内容
。然后您可以看到文件夹。
您还可以通过项目中的路径访问文件。
英文:
> I am not able to find the downloaded file in my apple device.
If you use a simulator, you can directly find the folder in the mac by entering the path in the
your mac
--> select Finder
--> Then open the Go
menu --> Click Go to Folder
as I described in this thread:
Where can I find the MyDocuments folder on iPhone Simulator?
If you installed the app in a real device. You can browse the file in
Xcode
--> Devices and Simulators
--> download container
as described in this thread:
Browse the files created on a device by the iOS application I'm developing, after downloading the container, you can right click it and choose Show Package Contents
. Then you can see the folders.
You can also access the file by the path in the project.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论