Visual Studio Community 2017/ Xamarinを使用。アプリ内で作成・保存したテキストファイルをメールに添付して送信するシステムを作っています。
Android7では、アプリでファイルを作成して、Gmailに添付するなどの処理を行おうとすると、FileUriExposedExceptionエラーが発生します。この問題をクリアするために、AndroidManifest.xmlにproviderの定義を、res/xml/filepaths.xmlに使用するパスを設定し、file:// uriをcontent:// uriに置き換えることによって、これらの問題をクリアしている報告を目にします。しかし、これらのサイトの情報はAndroid Studio上でJavaで書かれたコードです。Xamarinによる報告は見当たりません。私なりに下のようなコードを考えましたが、エラーになります。
よろしくアドバイスをお願いします。
参考:
https://codeday.me/jp/qa/20181212/63613.html
https://sankame.github.io/blog/2018-07-23-android_use_fileprovider/
エラーメッセージ:
Unhandled Exception: Java.Lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.ContentFrameLayout.setId(int)' on a null object reference
該当のソースコード
C# Xamarin
private void FileSend_Clicked(object sender, EventArgs e) { bool result = DependencyService.Get<IMailService>().ApMailer(“data.pdf”); } public interface IMailService { bool ApMailer(string file); } ### AndroidManifest.xml … <application … > <provider android:name="android.support.v4.content.FileProvider" android:authorities="${applicationId}.fileprovider" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/filepaths" /> </provider> </application> … ### Resource/xml/filepaths.xml <paths xmlns:android="http://schemas.android.com/apk/res/android"> <files-path name="share_name" path="."/> </paths> ### MailService.cs public class MailService : IMailService { public bool ApMailer(string file) { Context context = Android.App.Application.Context; Android.Net.Uri filePath = FileProvider.GetUriForFile(context, ".fileprovider", new Java.IO.File(file)); Intent intent = new Intent(); intent.SetAction(Intent.ActionSend); intent.AddFlags(ActivityFlags.NewTask); intent.SetDataAndType(filePath, "message/rfc822"); Android.App.Application.Context.StartActivity(intent); return true; } }
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/03/22 05:10
2019/03/22 05:43
2019/03/22 08:27
2019/03/22 09:22
2019/03/22 10:12