我正在尝试添加通过其他应用程序共享文件的可能性,但我不能这样做。
我感兴趣的是能够通过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..."));
转换为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();
}
}
谢谢!
答案 0 :(得分:2)
我感兴趣的是能够通过Whatsapp和电报做到这一点,但是当我尝试它时会说“不报告的格式”或类似的错误。
从您的代码中,您尝试共享一个大字节数组,既不支持也不推荐。正确的方法是将音频文件从内部存储器复制到外部存储器并将其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();
}
}
}
更新
如果音频文件位于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);
注意:如果Visual Studio没有为Resource.Designer.cs
更新Resource.Raw.resourceName
,您可以手动更新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()
{
}
}
}
重建项目时,部分类Raw
不会被覆盖。