嗨,我正在尝试创建文件并将其附加到电子邮件请求,但我不断收到错误消息
无法执行android:onClick的方法
摆脱行emailIntent.putExtra(Intent.EXTRA_STREAM, path);
摆脱问题,但电子邮件丢失附件。通过错误测试,我的文件已经制作完成,并且路径确实存在,但是不会附加到我的电子邮件中
下面是我要运行的代码
public void emailPpl(View v){
///////////////////////////////////////Create File//////////////////////////////
String filename = "data.sch";
String eventName= getIntent().getStringExtra("eventName");
String Location= getIntent().getStringExtra("Location");
String starTime= getIntent().getStringExtra("startTime");
String endTime= getIntent().getStringExtra("endTime");
String date= getIntent().getStringExtra("date");
String fileContents = eventName + "," + Location + "," + date + "," + starTime + "," + endTime + "\n";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_APPEND);
outputStream.write(fileContents.getBytes());
outputStream.close();
Toast.makeText(this, "write complete",
Toast.LENGTH_LONG).show();
System.out.print("Success gg");
} catch (Exception e) {
e.printStackTrace();
}
//////////////////////////////////////////////////////////////////////////////
////////////////////////Begin Email Request//////////////////////////////////
try{
Log.i("Send email", "");
EditText emails= findViewById(R.id.editText2);
EditText Message= findViewById(R.id.editText4);
String[] TO = emails.getText().toString().split(",");
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
File filelocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename);
Uri path = Uri.fromFile(filelocation);
Log.d("path","file path confirmed "+ path);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, Message.getText().toString()+"\n Thank you for using Family Scheduler");
if (path != null) {
emailIntent.putExtra(Intent.EXTRA_STREAM, path);
}
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
Log.i("Finished sending email", "");
Intent intent = new Intent(this, addevent.class);
intent.putExtra("TO",TO);
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(this,
"There is no email client installed.", Toast.LENGTH_SHORT).show();
}
}
这是错误
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.wasi.familyschedualer, PID: 28031
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: android.os.FileUriExposedException: file:///storage/emulated/0/data.sch exposed beyond app through ClipData.Item.getUri()
at android.os.StrictMode.onFileUriExposed(StrictMode.java:1960)
at android.net.Uri.checkFileUriExposed(Uri.java:2356)
at android.content.ClipData.prepareToLeaveProcess(ClipData.java:942)
at android.content.Intent.prepareToLeaveProcess(Intent.java:9850)
at android.content.Intent.prepareToLeaveProcess(Intent.java:9856)
at android.content.Intent.prepareToLeaveProcess(Intent.java:9835)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1610)
at android.app.Activity.startActivityForResult(Activity.java:4487)
at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
at android.app.Activity.startActivityForResult(Activity.java:4445)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:720)
at android.app.Activity.startActivity(Activity.java:4806)
at android.app.Activity.startActivity(Activity.java:4774)
at com.example.wasi.familyschedualer.invitingPpl.emailPpl(invitingPpl.java:64)