Xamarin Forms for Android,如何共享和打开文件?

问题描述 投票:0回答:1

我是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"));

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

其次,我想出了如何打开文件并获取其路径的方法:受保护的重写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.datSdCard中的文件路径/document/1513-1812:MyFile.dat

如何打开这些文件?或者如何将它们作为字节[]获得?

谢谢您,我会很高兴。

xamarin.forms xamarin.android
1个回答
0
投票

下载文件夹中的文件路径/document/raw:/storage/emulated/0/Download/MyFile.dat SdCard中的文件路径/document/1513-1812:MyFile.dat。如何打开这些文件?或者如何将它们作为字节[]获得?

如果要从Xamarin.Forms的Download文件夹中打开文件,则可以使用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);
    }
}

}

最后,您可以通过以下代码获取字节[]:

 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);
    }
}

}

最后,您可以获得字节[]:

 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);             
        }


    }
© www.soinside.com 2019 - 2024. All rights reserved.