使用电子邮件意图添加附件 Android
Adding attachment using email intent Android
我已将电子邮件意图添加到 Android 应用程序,其中包含将本地文件添加为附件的代码。
但是当我点击 "Email Data" 按钮打开 Intent 时,应用程序崩溃了
日志cat显示如下,http://hastebin.com/idejavunam.avrasm,这一行输出空指针异常错误:
case R.id.emailBtn:
所以我认为它是文件 uri 的问题,但由于该文件存在于设备的文件系统中,所以不明白为什么。
有谁知道如何调试这个问题?
可能是我将文件的路径错误地传递给了电子邮件意图?
这是我实施解决方案所遵循的过程。
来自创建 csv 文件的方法的代码:
String baseDir = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "AnalysisData.csv";
//this filePath is used in email code and converted to Uri.
filePath = baseDir + File.separator + fileName;
File f = new File(filePath);
这是调用电子邮件意图的代码,文件路径转换为用于附件目的的 URI:
case R.id.emailBtn: {
Toast.makeText(this, "email clicked", Toast.LENGTH_SHORT).show();
Uri.fromFile(new File(filePath));
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","abc@gmail.com", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "EXTRA_SUBJECT");
emailIntent.putExtra(Intent.EXTRA_STREAM, filePath);
startActivity(Intent.createChooser(emailIntent, "Send email..."));
break;
我已经修改了部分检查,如果现在可以的话。
case R.id.emailBtn: {
Toast.makeText(this, "email clicked", Toast.LENGTH_SHORT).show();
Uri uri = Uri.fromFile(new File(filePath));
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","abc@gmail.com", null));
emailIntent.setType("*/*");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "EXTRA_SUBJECT");
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(emailIntent, "Send email..."));
break;
更新
另外,在查看 logcat 之后,我发现您的 文件路径为 null 。请更正
编辑
我已经修改了你的 onClick 方法,只需更换告诉我它是否适合你
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String baseDir = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "AnalysisData.csv";
filePath = baseDir + File.separator + fileName;
File f = new File(filePath);
switch (v.getId()) {
case R.id.exportBtn: {
Toast.makeText(this, "select clicked", Toast.LENGTH_SHORT).show();
//write sample data to csv file using open csv lib.
date = new Date();
CSVWriter writer = null;
// File exist
if(f.exists() && !f.isDirectory()){
FileWriter mFileWriter = null;
try {
mFileWriter = new FileWriter(filePath , true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
writer = new CSVWriter(mFileWriter);
}
else {
try {
writer = new CSVWriter(new FileWriter(filePath));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String data [] = new String[] {"Record Number","Ship Name","Scientist Name","Scientist Email","Sample Volume","Sample Colour","Sample Material","Latitude","Longitude","Date","\r\n"};
writer.writeNext(data);
/*
//retrieve record cntr from prefs
SharedPreferences settings = getSharedPreferences("RECORD_PREF", 0);
recordCntr = settings.getInt("RECORD_COUNT", 0); //0 is the default value
*/
//increment record count
recordCntr++;
/*
//save record cntr from prefs
settings = getSharedPreferences("RECORD_PREF", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("RECORD_COUNT",recordCntr);
editor.commit();
*/
data = new String[]{Integer.toString(recordCntr),shipName,analystName,analystEmail,sampleVolume,
sampleColour,sampleMaterial,latitudeValue.toString(),longitudeValue.toString(),new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date),"\r\n"};
writer.writeNext(data);
try {
writer.close();
Toast.makeText(this, "Data exported succesfully!", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, "Error exporting data!", Toast.LENGTH_SHORT).show();
}
break;
}
case R.id.emailBtn: {
Toast.makeText(this, "email clicked", Toast.LENGTH_SHORT).show();
if (f.exists() && !f.isDirectory()) {
Uri uri = Uri.fromFile(f);
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","abc@gmail.com", null));
emailIntent.setType("*/*");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "EXTRA_SUBJECT");
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(emailIntent, "Send email..."));
}
break;
}
}
}
我已将电子邮件意图添加到 Android 应用程序,其中包含将本地文件添加为附件的代码。
但是当我点击 "Email Data" 按钮打开 Intent 时,应用程序崩溃了 日志cat显示如下,http://hastebin.com/idejavunam.avrasm,这一行输出空指针异常错误:
case R.id.emailBtn:
所以我认为它是文件 uri 的问题,但由于该文件存在于设备的文件系统中,所以不明白为什么。
有谁知道如何调试这个问题? 可能是我将文件的路径错误地传递给了电子邮件意图?
这是我实施解决方案所遵循的过程。
来自创建 csv 文件的方法的代码:
String baseDir = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "AnalysisData.csv";
//this filePath is used in email code and converted to Uri.
filePath = baseDir + File.separator + fileName;
File f = new File(filePath);
这是调用电子邮件意图的代码,文件路径转换为用于附件目的的 URI:
case R.id.emailBtn: {
Toast.makeText(this, "email clicked", Toast.LENGTH_SHORT).show();
Uri.fromFile(new File(filePath));
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","abc@gmail.com", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "EXTRA_SUBJECT");
emailIntent.putExtra(Intent.EXTRA_STREAM, filePath);
startActivity(Intent.createChooser(emailIntent, "Send email..."));
break;
我已经修改了部分检查,如果现在可以的话。
case R.id.emailBtn: {
Toast.makeText(this, "email clicked", Toast.LENGTH_SHORT).show();
Uri uri = Uri.fromFile(new File(filePath));
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","abc@gmail.com", null));
emailIntent.setType("*/*");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "EXTRA_SUBJECT");
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(emailIntent, "Send email..."));
break;
更新
另外,在查看 logcat 之后,我发现您的 文件路径为 null 。请更正
编辑
我已经修改了你的 onClick 方法,只需更换告诉我它是否适合你
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String baseDir = android.os.Environment.getExternalStorageDirectory().getAbsolutePath();
String fileName = "AnalysisData.csv";
filePath = baseDir + File.separator + fileName;
File f = new File(filePath);
switch (v.getId()) {
case R.id.exportBtn: {
Toast.makeText(this, "select clicked", Toast.LENGTH_SHORT).show();
//write sample data to csv file using open csv lib.
date = new Date();
CSVWriter writer = null;
// File exist
if(f.exists() && !f.isDirectory()){
FileWriter mFileWriter = null;
try {
mFileWriter = new FileWriter(filePath , true);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
writer = new CSVWriter(mFileWriter);
}
else {
try {
writer = new CSVWriter(new FileWriter(filePath));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
String data [] = new String[] {"Record Number","Ship Name","Scientist Name","Scientist Email","Sample Volume","Sample Colour","Sample Material","Latitude","Longitude","Date","\r\n"};
writer.writeNext(data);
/*
//retrieve record cntr from prefs
SharedPreferences settings = getSharedPreferences("RECORD_PREF", 0);
recordCntr = settings.getInt("RECORD_COUNT", 0); //0 is the default value
*/
//increment record count
recordCntr++;
/*
//save record cntr from prefs
settings = getSharedPreferences("RECORD_PREF", 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("RECORD_COUNT",recordCntr);
editor.commit();
*/
data = new String[]{Integer.toString(recordCntr),shipName,analystName,analystEmail,sampleVolume,
sampleColour,sampleMaterial,latitudeValue.toString(),longitudeValue.toString(),new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date),"\r\n"};
writer.writeNext(data);
try {
writer.close();
Toast.makeText(this, "Data exported succesfully!", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(this, "Error exporting data!", Toast.LENGTH_SHORT).show();
}
break;
}
case R.id.emailBtn: {
Toast.makeText(this, "email clicked", Toast.LENGTH_SHORT).show();
if (f.exists() && !f.isDirectory()) {
Uri uri = Uri.fromFile(f);
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto","abc@gmail.com", null));
emailIntent.setType("*/*");
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "EXTRA_SUBJECT");
emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
startActivity(Intent.createChooser(emailIntent, "Send email..."));
}
break;
}
}
}