【问题标题】:Xamarin Forms for Android, how to share and open a file?Xamarin Forms for Android,如何共享和打开文件?
【发布时间】:2020-08-02 01:10:59
【问题描述】:

我是 Xamarin 的新手,并试图弄清楚。有两点我不能理解。 首先,我正在尝试共享一个文件:

byte[] fileArray = ....
var sharingIntent = new Intent(Intent.ActionSend);
sharingIntent.SetType("application/octet-stream");
sharingIntent.PutExtra(Intent.ExtraStream, fileArray);
StartActivity(Intent.CreateChooser(sharingIntent, "Send file"));

我收到一个错误:加载错误。该请求不包含任何数据。

其次,我想出了如何打开一个文件并获得它的路径: protected override void OnActivityResult(int requestCode, Result resultCode, Intent intent)

protected override void OnActivityResult(int requestCode, Result resultCode, Intent intent)
{
    base.OnActivityResult(requestCode, resultCode, intent);
    switch (resultCode)
    {
        case Result.Ok:
            string path = intent.Data.Path;
         break;
    }
}

下载文件夹中文件的路径 /document/raw:/storage/emulated/0/Download/MyFile.dat SdCard 中的文件路径 /document/1513-1812:MyFile.dat

如何打开这些文件? 或者我怎样才能将它们作为 Byte []?

我很乐意为您提供任何帮助,谢谢。

【问题讨论】:

    标签: xamarin.forms xamarin.android


    【解决方案1】:

    Downloads 文件夹中文件的路径 /document/raw:/storage/emulated/0/Download/MyFile.dat SdCard 中的文件路径 /document/1513-1812:MyFile.dat.如何打开这些文件?或者我怎样才能将它们作为 Byte []?

    如果您想从 Xamarin.Forms 中的下载文件夹打开文件,您可以使用 DependencyService 来执行此操作。

    首先,在 PCL 中创建类。

    public interface IFileSystem
    {
    
        byte[] ReadAllByteS();
    
    }
    

    然后在android中实现:

    [assembly: Dependency(typeof(FileSystemImplementation))]
    namespace demo3.Droid
    {
    public class FileSystemImplementation : IFileSystem
    {
    
        public byte[] ReadAllByteS()
        {
            if (ContextCompat.CheckSelfPermission(MainActivity.mactivity, Manifest.Permission.ReadExternalStorage) != (int)Permission.Granted)
            {
                ActivityCompat.RequestPermissions(MainActivity.mactivity, new String[] { Manifest.Permission.ReadExternalStorage }, 1);
    
                return null;
            }
            var filePath = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDownloads);
            var path = System.IO.Path.Combine(filePath.AbsolutePath, "testfile/hello.txt");
            return File.ReadAllBytes(path);
        }
    }
    

    }

    最后可以通过以下代码获取byte[]:

     private void btn1_Clicked(object sender, EventArgs e)
        {
    
            byte[] dataBuffer = DependencyService.Get<IFileSystem>().ReadAllByteS();
    
        }
    

    更新:

    如果你想选择一个文件并获取这个文件路径打开,我建议你可以使用Xamarin.Plugin.FilePicker来选择文件并获取文件路径。

    首先,通过nuget包安装Xamarin.Plugin.FilePicker...

    然后在 PCL 中修改 IFileSystem 类:

    public interface IFileSystem
    {
    
        byte[] ReadAllByteS(string path);
    
    }
    

    和 Android 中的 FileSystemImplementation 类:

    [assembly: Dependency(typeof(FileSystemImplementation))]
    namespace demo3.Droid
    {
    public class FileSystemImplementation : IFileSystem
    {
    
        public byte[] ReadAllByteS(string path)
        {
            if (ContextCompat.CheckSelfPermission(MainActivity.mactivity, Manifest.Permission.ReadExternalStorage) != (int)Permission.Granted)
            {
                ActivityCompat.RequestPermissions(MainActivity.mactivity, new String[] { Manifest.Permission.ReadExternalStorage }, 1);
    
                return null;
            }
            //var filePath = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDownloads);
            //var path = System.IO.Path.Combine(filePath.AbsolutePath, "testfile/hello.txt");
            return File.ReadAllBytes(path);
        }
    }
    

    }

    最后可以得到byte[]:

     private async void btn1_Clicked(object sender, EventArgs e)
        {
            FileData file = await CrossFilePicker.Current.PickFile();
    
            if (file != null)
            {
    
                string path = file.FilePath;
                byte[] dataBuffer = DependencyService.Get<IFileSystem>().ReadAllByteS(path);             
            }
    
    
        }
    

    【讨论】:

    • 感谢您的回答。一切都清楚了: var filePath = Environment.GetExternalStoragePublicDirectory(Environment.DirectoryDownloads);但如果是任意路径,例如用 SD 卡打开文件呢?
    • @Neff 如果你想选择一个文件并打开这个文件路径,请看我的更新。
    • 谢谢。我正在使用 Xamarin.Plugin.FilePicker,我只是想了解如何在没有插件的情况下实现这一点。此外,Xamarin.Plugin.FilePicker 在真实设备上并不总是正常工作,例如,它无法从真实设备上的下载文件夹中打开文件。
    • @Neff 我使用 Xamarin.Plugin.FilePicker 成功打开了下载文件夹中的文件。如果你不使用Plugin来选择,你可以在Android中创建文件选择器:github.com/xamarin/docs-archive/tree/master/Recipes/android/…
    • 感谢您的参与。我想我正在尝试制作我的 FilePicker,这将是一次很好的体验。
    猜你喜欢
    • 1970-01-01
    • 2022-01-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多