使用电子邮件意图Android添加附件

时间:2015-01-08 14:34:50

标签: android file email android-intent uri

我已向Android应用添加了电子邮件意图,其中包含将本地文件添加为附件的代码。

但是当我点击"电子邮件数据"按钮打开意图我得到一个应用程序崩溃 和log 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;

1 个答案:

答案 0 :(得分:1)

如果现在有效,我修改了部分检查。

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后,我发现你的文件路径为空。善意地纠正

修改

我修改了你的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;
        }
    }

}