In Xamarin.Forms, how can I copy a file from the isolated storage to the Downloads folder?

时间秒杀一切 提交于 2019-12-24 06:35:42

问题


I'm trying to copy my database file from the isolated storage to the Download folder (or any folder that the user can access).

Currently my database is stored in:

/data/user/0/com.companyname.appname/files/Databases/MyDatabase.db

I tried to use this code:

public string GetCustomFilePath(string folder, string filename)
{
    var docFolder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
    var libFolder = Path.Combine(docFolder, folder);

    if (!Directory.Exists(libFolder))
        Directory.CreateDirectory(libFolder);

    return Path.Combine(libFolder, filename);
}


var bas = GetDatabaseFilePath("MyDatabase.db");
var des = Path.Combine(Android.OS.Environment.DirectoryDownloads, "MyDatabase.db");
File.Copy(bas, des);

The Android.OS.Environment.DirectoryDownloads property returns the path Download, which is the name of the downloads folder.
But File.Copy() throws an exception telling

System.IO.DirectoryNotFoundException: Destination directory not found: Download.

I tried to use a slash before like this: /Download/MyDatabase.db with no luck.

Is there any way to copy a file like that? Do I need any permission?


回答1:


1st) Yes, you do need permissions to write to external storage.

You can get the runtime time permission required by doing it yourself:

  • https://devblogs.microsoft.com/xamarin/requesting-runtime-permissions-in-android-marshmallow/

Or via a 3rd-party plugin, such as James Montemagno's PermissionsPlugin

  • https://github.com/jamesmontemagno/PermissionsPlugin

2nd) Once your user accepts that it is ok to write to external storage, you can use:

Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads

To obtain the path of the device's public Download folder, i.e. using a Forms' dependency service:

public interface IDownloadPath
{
    string Get(); 
}

public class DownloadPath_Android : IDownloadPath
{
    public string Get()
    {
        return Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, Android.OS.Environment.DirectoryDownloads);
    }
}
  • https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/introduction

You end up with something like:

public void Handle_Button(object sender, System.EventArgs e)
{
    var fileName = "someFile.txt";
    using (var stream = File.Create(Path.Combine(FileSystem.CacheDirectory, fileName)))
    {
       // just creating a dummy file to copy (in the cache dir using Xamarin.Essentials
    }


    var downloadPath = DependencyService.Get<IDownloadPath>().Get();
    File.Copy(Path.Combine(FileSystem.CacheDirectory, fileName), downloadPath);
}


来源:https://stackoverflow.com/questions/56875215/in-xamarin-forms-how-can-i-copy-a-file-from-the-isolated-storage-to-the-downloa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!