0

I've added an email intent to an Android app with code to add a local file as an attchment.

But when I click "Email Data" button to open the intent I get a an app crash and log cat shows the following, http://hastebin.com/idejavunam.avrasm , an error of null pointer exception is output at this line:

case R.id.emailBtn:

so I thought its a problem with the file uri but can't see why as the file exists in the device's file system.

Does anyone know how I can debug this issue? Possibly I'm passing the file's path to email intent incorrectly?

This is the process I'm following to implement the solution.

code from the method that creates csv file:

        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);

And this is the code where the email intent is called, with the file path converted to a Uri for attachment prposes:

 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","[email protected]", null));
             emailIntent.putExtra(Intent.EXTRA_SUBJECT, "EXTRA_SUBJECT");
             emailIntent.putExtra(Intent.EXTRA_STREAM, filePath);
             startActivity(Intent.createChooser(emailIntent, "Send email..."));


            break;

1 Answer 1

1

I have modified some part check, if it works now.

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","[email protected]", null)); 
             emailIntent.setType("*/*");
             emailIntent.putExtra(Intent.EXTRA_SUBJECT, "EXTRA_SUBJECT");
             emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
             startActivity(Intent.createChooser(emailIntent, "Send email..."));


            break;

UPDATE

Also after looking at the logcat I found that your filepath is null . kindly correct that

EDIT

I have modified your onClick Method simply replace tell me if it works for you

@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","[email protected]", null));
                emailIntent.setType("*/*");
                emailIntent.putExtra(Intent.EXTRA_SUBJECT, "EXTRA_SUBJECT");
                emailIntent.putExtra(Intent.EXTRA_STREAM, uri);
                startActivity(Intent.createChooser(emailIntent, "Send email..."));
            }


            break;
        }
    }

}
Sign up to request clarification or add additional context in comments.

3 Comments

kindly find the update to locate the error , also modify your code to match the provided code.
@BrianJ since it is difficult to get the flow from the above code pieces can u share your complete java file ?
This works now when I change code from Intent.ACTION_SENDTO to Intent.ACTION_SEND, can you please remove my code for exportBtn click before I accept answer as this does not relate to the question?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.