私は共通のオープンルーチンを書いて、以下のような個々のファイルタイプを見つけました。私は次のことをした後に問題はなかった。うまくいけば、これは役に立ちます。 (注 - 私は単語の呼び出しを追加した - しかし、このすべてのAndroidプロジェクトに置き
public static Java.IO.File CopyDocuments(Java.IO.File source, string realName)
{
//string path = System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments); <-- old method
string path = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads).AbsolutePath;
if (!System.IO.Directory.Exists(Path.Combine(path, "appname")))
System.IO.Directory.CreateDirectory(Path.Combine(path, "appname"));
string dbPath = Path.Combine(path,"appname", realName);
Java.IO.File destination = new Java.IO.File(dbPath);
try
{
//if (destination.Exists())
// destination.Delete();
if (!destination.Exists())
{
using (FileStream fs = new FileStream(source.AbsolutePath, FileMode.Open, FileAccess.Read))
{
using (var br = new BinaryReader(fs))
{
using (var bw = new BinaryWriter(new FileStream(dbPath, FileMode.Create)))
{
byte[] buffer = new byte[2048];
int length = 0;
while ((length = br.Read(buffer, 0, buffer.Length)) > 0)
{
bw.Write(buffer, 0, length);
}
}
}
}
}
return destination;
}
catch (Exception ex)
{
Toast.MakeText(Xamarin.Forms.Forms.Context, "Error Copying: " + ex.Message, ToastLength.Long).Show();
}
return null;
}
public void LaunchApp(string fileLocation, string realName)
{
var file = new Java.IO.File(fileLocation);
if (!file.Exists())
return;
file = CopyDocuments(file, realName);
Intent intent = null;
var extension = System.IO.Path.GetExtension(fileLocation).ToLower();
switch (extension)
{
case ContentService.DocxExtension:
intent = ReturnWord(file, true);
break;
case ContentService.DocExtension:
intent = ReturnWord(file, false);
break;
case ContentService.TxtExtension:
case PlayerLync.Services.ContentService.RtfExtension:
intent = ReturnText(file);
break;
case ContentService.XlsExtension:
intent = ReturnExcel(file, false);
break;
case ContentService.XlsxExtension:
intent = ReturnExcel(file, true);
break;
case ContentService.PPExtension:
intent = ReturnPowerPoint(file, false);
break;
case ContentService.PPXExtension:
intent = ReturnPowerPoint(file, true);
break;
case ContentService.Mp3Extension:
//contentType = ContentType.Audio;
break;
default:
//contentType = ContentType.Unknown;
break;
}
try
{
Xamarin.Forms.Forms.Context.StartActivity(intent);
}
catch (Exception ex)
{
... log error
}
}
private Intent ReturnWord(Java.IO.File file, bool isEx)
{
var intent = new Intent(Intent.ActionView);
Android.Net.Uri uri = FileProvider.GetUriForFile(Xamarin.Forms.Forms.Context, "your_package.fileprovider", file);// --> old method Android.Net.Uri.FromFile(file); //
intent.AddFlags(ActivityFlags.GrantReadUriPermission);
intent.AddFlags(ActivityFlags.GrantWriteUriPermission);
intent.AddFlags(ActivityFlags.GrantPersistableUriPermission);
intent.PutExtra(Intent.ExtraStream, uri);
if (!isEx)
{
intent.SetDataAndType(uri, "application/vnd.msword");
}
else
{
intent.SetDataAndType(uri, "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
}
return intent;
}
ここでSOの記事(What is a correct mime type for docx, pptx etc?)から型を使用し、Xamarinは
フォームからアクセスするためのカスタムレンダラを行います