Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to attach file to the email from /data folder of the device

Tags:

android

I am trying to attach the file from the /data folder of the device.

I have successfully created "abc.txt" in /data folder, I can see the file at that place.

I am using below code to send email:

    Intent intent = new Intent(Intent.ACTION_SEND);
    intent.setType("text/plain");
    intent.putExtra(Intent.EXTRA_EMAIL, new String[]{[email protected]});
    intent.putExtra(Intent.EXTRA_STREAM,
       Uri.parse(Environment.getDataDirectory()+"/abc.txt"));
    intent.putExtra(Intent.EXTRA_TEXT, "hello..");
    startActivity(Intent.createChooser(intent, email_chooser_title));

but I am unable to receive the attachments..

pl. let me know what's the mistake I have done..

thanks.

like image 933
brig Avatar asked Jun 30 '11 15:06

brig


People also ask

How do I attach a folder of files to an email?

Right click on the folder itself. In the menu that pops up, choose “Send to”, then choose “Compressed (zipped) folder” Rename the zipped folder if necessary, then hit enter. Right click the zipped folder, then choose “Send to” again, but this time choose “Mail Recipient”

How do I email a document from my files?

Send as the body of an email message Open the file you want to send. In the Quick Access Toolbar, click Send to Mail Recipient to open an email message. Your file will appear in the body of the message. Enter the recipients' aliases, edit the subject line and message body as necessary, and then click Send.

How do I attach a file in Outlook not in the body?

In a new email message, select the Format Text tab in the ribbon. Select Plain Text or Rich Text. Select the Message tab in the ribbon and then select Attach File.


2 Answers

You must copy the file to the external directory (aka SD Card). It's because the email application cannot access your data directory (in the same way that you can't access other app's data directory)

like image 116
Cristian Avatar answered Nov 11 '22 11:11

Cristian


Try using this code:

File sdCard = Environment.getExternalStorageDirectory();
PATH=sdCard.toString()+"/abc.txt";
Intent intent=new Intent(Intent.ACTION_SEND);
intent.setType("**/**");// or intent.setType("text/plain");
String[] recipients={"[email protected]");
intent.putExtra(Intent.EXTRA_EMAIL, recipients);
intent.putExtra(Intent.EXTRA_SUBJECT,getResources().getString(R.string.app_name));
intent.putExtra(Intent.EXTRA_STREAM,Uri.parse("file://"+PATH));
intent.putExtra(Intent.EXTRA_TEXT, "hello..");
startActivity(Intent.createChooser(intent,""));

Make sure there is abc.txt file in your Device/Emulator's sdcard.

Also include these two permissions in your project's manifest file:

<uses-permission  android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
like image 33
AkashG Avatar answered Nov 11 '22 11:11

AkashG