简体   繁体   English

共享原始资源Xamarin Android

[英]Share raw resources Xamarin Android

I'm trying to add the possibility of sharing files through other applications, but I can not do it. 我试图增加通过其他应用程序共享文件的可能性,但是我做不到。

I am interested in being able to do it through Whatsapp and telegram, but when I try it says "unsuported format" or similar errors. 我对能够通过Whatsapp和电报实现此功能很感兴趣,但是当我尝试使用它时却说“无格式”或类似的错误。

            System.IO.Stream inputStream= data.context.Resources.OpenRawResource(data.context.Resources.GetIdentifier(path, "raw", data.context.ApplicationContext.PackageName));

            var sharingIntent = new Intent();
            sharingIntent.SetAction(Intent.ActionSend);
            sharingIntent.PutExtra(Intent.ExtraStream, ReadFully(inputStream));
            sharingIntent.SetType("audio/*");

            data.context.StartActivity(Intent.CreateChooser(sharingIntent, "Share..."));

And the conversion to byte[] 并转换为byte []

     public static byte[] ReadFully(System.IO.Stream input)
    {
        byte[] buffer = new byte[16 * 1024];
        using (System.IO.MemoryStream ms = new System.IO.MemoryStream())
        {
            int read;
            while ((read = input.Read(buffer, 0, buffer.Length)) > 0)
            {
                ms.Write(buffer, 0, read);
            }
            return ms.ToArray();
        }
    }

Thank you! 谢谢!

I am interested in being able to do it through Whatsapp and telegram, but when I try it says "unsuported format" or similar errors. 我对能够通过Whatsapp和电报实现此功能很感兴趣,但是当我尝试使用它时却说“无格式”或类似的错误。

From your codes, you are trying to share a big byte array, which is neither supported nor recommended. 根据您的代码,您试图共享一个大字节数组,既不支持也不建议这样做。 The correct way to do that is to copy the audio file from internal storage to external storage and share the Uri of it to other app: 正确的方法是将音频文件从内部存储复制到外部存储,并将其Uri共享到其他应用程序:

protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);

        // Set our view from the "main" layout resource
        SetContentView (Resource.Layout.Main);

        //Create a new file in the exteranl storage and copy the file from assets folder to external storage folder
        Java.IO.File dstFile = new Java.IO.File(Environment.ExternalStorageDirectory.Path + "/TestSound.mp3");
        dstFile.CreateNewFile();
        var inputStream = new FileInputStream(Assets.OpenFd("fileName.mp3").FileDescriptor);
        var outputStream = new FileOutputStream(dstFile);
        CopyFile(inputStream,outputStream);

        //to let system scan the audio file and detect it
        Intent intent = new Intent(Intent.ActionMediaScannerScanFile);
        intent.SetData(Uri.FromFile(dstFile));
        this.SendBroadcast(intent);

        //share the Uri of the file
        var sharingIntent = new Intent();
        sharingIntent.SetAction(Intent.ActionSend);
        sharingIntent.PutExtra(Intent.ExtraStream, Uri.FromFile(dstFile));
        sharingIntent.SetType("audio/*");

        this.StartActivity(Intent.CreateChooser(sharingIntent, "Share..."));
    }

    public void CopyFile(FileInputStream inputStream,FileOutputStream outputStream)
    {
        //FileChannel inChannel = new FileInputStream(src).Channel;
        FileChannel inChannel = inputStream.Channel;
        FileChannel outChannel = outputStream.Channel;
        try
        {
            inChannel.TransferTo(0, inChannel.Size(), outChannel);
        }
        finally
        {
            if (inChannel != null)
            {
                inChannel.Close();
            }

            if (outChannel != null)
            {
                outChannel.Close();
            }
        }
    }

Update: 更新:

If the audio files are in Resources/raw folder, you can use following codes to get the FileInputStream: 如果音频文件位于Resources / raw文件夹中,则可以使用以下代码来获取FileInputStream:

//My_Heart_Will_Go_On.mp3 in Resources/raw folder
AssetFileDescriptor descripter = this.Resources.OpenRawResourceFd(Resource.Raw.My_Heart_Will_Go_On);
var inputStream = new FileInputStream(descripter.FileDescriptor);

Notes: if Visual Studio didn't update Resource.Designer.cs for Resource.Raw.resourceName , you can manually update the Resource.Designer.cs like below: 注意:如果Visual Studio没有为Resource.Raw.resourceName更新Resource.Designer.cs ,则可以手动更新Resource.Designer.cs如下所示:

[System.CodeDom.Compiler.GeneratedCodeAttribute("Xamarin.Android.Build.Tasks", "1.0.0.0")]
public partial class Resource
{
   ...
   public partial class Raw
    {

        static Raw()
        {
            global::Android.Runtime.ResourceIdManager.UpdateIdValues();
        }

        private Raw()
        {
        }
    }
}

Partial class Raw won't get overrided when rebuild the project. 重建项目时不会覆盖局部类Raw

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM