Domanda

hi im trying to create an app that creates a word file and sends it through the mail. so far i managed to create the file, and the necessary intent for invoking a mail app chooser, however i encountered 2 issues:

1)i receive a list containing more than mail programs - wifi, BT etc show as well

2)if i choose gmail see the file name as an attachment in gmail however after sending it i receive no file. i tried to swich to using URI instead of Uri but then i get javaNullExeption when gmail try to attach the file.

the question is:how to send the attachment (without use of complicated classes like content providers if possible)

the code i used:

to create the file:

public void saveFile(String fileName,String content) throws IOException,FileNotFoundException
    {
        FileOutputStream fos = getContext().openFileOutput(fileName, Context.MODE_WORLD_READABLE);
        fos.write(content.getBytes());
        fos.close();    
    }

to send it:

public void sendAsMail(Context context,String fileName)
{
    File file = new File(getContext().getFilesDir().getAbsolutePath()+"/"+fileName);
    //file.setReadable(true);
    //URI myUri = file.toURI();
            Uri myUri=Uri.fromFile(file);
    Intent emailIntent = new Intent (Intent.ACTION_SEND);
    emailIntent.setType("text/plain");
    emailIntent.putExtra(Intent.EXTRA_EMAIL,"");
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, context.getResources().getString(R.string.free_search_mail_subject));
    emailIntent.putExtra(Intent.EXTRA_TEXT,context.getResources().getString(R.string.free_search_mail_content));
    emailIntent.putExtra(Intent.EXTRA_STREAM, myUri);
    context.startActivity(Intent.createChooser(emailIntent, "Send the file using:"));
}

i tested the file to see it is created properly (printed it using scanner) and it doesnt seem to be the problem

È stato utile?

Soluzione

First check file is created or not. If it is created, try using this code:

public static void sendAsMail(File file, Context econtext) {
    try {
        final Intent emailIntent = new Intent(
                android.content.Intent.ACTION_SEND);
        emailIntent.setType("text/*");
        emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,
                new String[] { "" });
        emailIntent.putExtra(android.content.Intent.EXTRA_CC,
                new String[] {});
        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,
                "FROM Sample");
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "HI");
        emailIntent.putExtra(android.content.Intent.EXTRA_STREAM,
                Uri.parse(file.toURI().toString()));
        mContext.startActivity(emailIntent);
    } catch (Exception e) {
    }
}

If it Doesn't works then comment.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top