ボタンを押すと、デフォルトのメールクライアントが開き、添付ファイルが開きます。
this をフォローしていますが、startActivityにエラーメッセージが表示されます。これは、意図を与えている間にアクティビティパラメーターを予期していることを示しています。私はAPI 21とAndroid Studio 1.1.0を使用しているので、おそらくリンクで提供される回答のコメントと関係があるのでしょうか?
Android開発者なので、本当に基本的なものが不足しているのでごめんなさい。
ここに私のコードがあります:
public void sendFileToEmail(File f){
String subject = "Lap times";
ArrayList<Uri> attachments = new ArrayList<Uri>();
attachments.add(Uri.fromFile(f));
Intent intent = new Intent(Intent.ACTION_SEND_MULTIPLE);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, attachments);
intent.setClassName("com.Android.email", "com.Android.mail.compose.ComposeActivity");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
e.printStackTrace();
}
問題は、正しいファイルパスを使用していないことだと思います。
次は私のために働く:
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, new String[] {"[email protected]"});
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "subject here");
emailIntent.putExtra(Intent.EXTRA_TEXT, "body text");
File root = Environment.getExternalStorageDirectory();
String pathToMyAttachedFile = "temp/attachement.xml";
File file = new File(root, pathToMyAttachedFile);
if (!file.exists() || !file.canRead()) {
return;
}
Uri uri = Uri.fromFile(file);
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(emailIntent, "Pick an Email provider"));
また、以下のようなマニフェストファイルを介してユーザーに許可を与える必要があります。
<uses-permission Android:name="Android.permission.WRITE_EXTERNAL_STORAGE"/>
これを使用してみてください。それは動作しています...
Intent emailIntent = new Intent(Android.content.Intent.ACTION_SEND);
emailIntent.setType("*/*");
emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(listVideos.get(position).getVideoPath())));//path of video
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
ありがとう
新しいデバイスの場合、FileUriExposedExceptionが発生します。 Kotlinでこれを回避する方法を次に示します。
val file = File(Environment.getExternalStorageDirectory(), "this")
val authority = context.packageName + ".provider"
val uri = FileProvider.getUriForFile(context, authority, file)
val emailIntent = createEmailIntent(uri)
startActivity(Intent.createChooser(emailIntent, "Send email..."))
private fun createEmailIntent(attachmentUri: Uri): Intent {
val emailIntent = Intent(Intent.ACTION_SEND)
emailIntent.type = "vnd.Android.cursor.dir/email"
val to = arrayOf("[email protected]")
emailIntent.putExtra(Intent.EXTRA_EMAIL, to)
emailIntent.putExtra(Intent.EXTRA_STREAM, attachmentUri)
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "subject")
return emailIntent
}