2012-04-02 44 views
36

Tôi muốn đính kèm tệp .vcf bằng thư của tôi và gửi qua thư. Nhưng thư được nhận trên địa chỉ mà không có tệp đính kèm. Tôi đã sử dụng mã bên dưới nhưng mã cho điều này và tôi không biết tôi đang ở đâu sai.Cách gửi email có tệp đính kèm trong Android

try {  
    String filelocation="/mnt/sdcard/contacts_sid.vcf";  
    Intent intent = new Intent(Intent.ACTION_SENDTO);  
    intent.setType("text/plain");  
    intent.putExtra(Intent.EXTRA_SUBJECT, "");  
    intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+filelocation));  
    intent.putExtra(Intent.EXTRA_TEXT, message);   
    intent.setData(Uri.parse("mailto:"));   
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

    activity.startActivity(intent); 
    activity.finish(); 
    } catch(Exception e) { 
    System.out.println("is exception raises during sending mail"+e); 
} 

Trả lời

69

Sử dụng mã dưới đây để gửi một email

String filename="contacts_sid.vcf"; 
File filelocation = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), filename); 
Uri path = Uri.fromFile(filelocation); 
Intent emailIntent = new Intent(Intent.ACTION_SEND); 
// set the type to 'email' 
emailIntent .setType("vnd.android.cursor.dir/email"); 
String to[] = {"[email protected]"}; 
emailIntent .putExtra(Intent.EXTRA_EMAIL, to); 
// the attachment 
emailIntent .putExtra(Intent.EXTRA_STREAM, path); 
// the mail subject 
emailIntent .putExtra(Intent.EXTRA_SUBJECT, "Subject"); 
startActivity(Intent.createChooser(emailIntent , "Send email...")); 
+0

xem một câu hỏi của tôi ... http: //stackoverflow.com/questions/12798001/android-how-to-send-multiple-contacts-are-attached-in-single-vcf-file- và gửi – NagarjunaReddy

+3

Bạn không nên sử dụng đường dẫn "được mã hóa cứng" vì chúng có thể thay đổi tùy theo thiết bị. Tôi đề nghị bạn thay đổi định nghĩa filelocation thành: File filelocation = new File (Environment.getExternalStorageDirectory(). GetAbsolutePath(), filename); Sau đó xác định: Uri path = Uri.fromFile (filelocation); và đặt nó vào mục đích của bạn: emailIntent .putExtra (Intent.EXTRA_STREAM, đường dẫn); –

+1

emailIntent.putExtra (Intent.EXTRA_STREAM, filelocation) sẽ không đính kèm tệp cho tôi, nhưng sử dụng emailIntent.putExtra (Intent.EXTRA_STREAM, Uri.parse ("file: //" + filelocation)) cũng như Phillip làm việc tốt. – andytrombone

4

Ví dụ trên chính thức Android site làm việc cho tôi. Tất cả những gì đang cần nó để thêm

startActivity(Intent.createChooser(emailIntent , "Send email...")); 

as done trong câu trả lời của Agarwal

+0

Trong trường hợp của tôi, nó sẽ gửi thư cho khách hàng nhưng không có tệp đính kèm. bánh mì nướng hiển thị là "không thể gửi tập tin trống". tệp của tôi được lưu trữ tại '/ data/data/com.example.app/files/temp.txt' và tôi chuyển nó bằng cách sử dụng' emailIntent.putExtra (Intent.EXTRA_STREAM, Uri.parse ("content: /" + filePath)); // filePath là /data/com.example.app/files/temp.txt ' – kAmol

+0

Bạn không thể gửi tệp vì tệp nằm trong thư mục bộ nhớ cache của ứng dụng và chỉ ứng dụng của bạn có thể đọc từ thư mục đó. Bạn nên sử dụng một thư mục khác, như Environment.getExternalStorageDirectory(). – Borzh

+0

Môi trường đã sử dụng.getExternalStorageDirectory(), đã xác minh rằng đường dẫn hợp lệ và tệp đó có dữ liệu tốt .... nhưng vẫn nhận được thông báo lỗi tương tự (?). – CESDewar

4

FOLDER_NAME là tên của tập tin trong Internal Storage của bạn của điện thoại. (ACTUALLY EXTERNAL_STORAGE). tên tệp là tên của tệp bạn muốn gửi.

private void ShareViaEmail(String folder_name, String file_name) { 
    try { 
     File Root= Environment.getExternalStorageDirectory(); 
     String filelocation=Root.getAbsolutePath() + folder_name + "/" + file_name; 
     Intent intent = new Intent(Intent.ACTION_SENDTO); 
     intent.setType("text/plain"); 
     String message="File to be shared is " + file_name + "."; 
     intent.putExtra(Intent.EXTRA_SUBJECT, "Subject"); 
     intent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+filelocation)); 
     intent.putExtra(Intent.EXTRA_TEXT, message); 
     intent.setData(Uri.parse("mailto:[email protected]")); 
     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 

     startActivity(intent); 
    } catch(Exception e) { 
     System.out.println("is exception raises during sending mail"+e); 
    } 
} 
1

SENDTO không hỗ trợ tệp đính kèm. Tôi đã thêm câu trả lời của mình bằng cách sử dụng Nhà cung cấp để đọc thông tin về tệp. Nó ở Kotlin.

fun shareFile(context: Context, filePath: File?, fileShareInfo: FileShareInfo) { 

    val intentFileShare = Intent(Intent.ACTION_SEND) 

    if (filePath!!.exists()) { 
     intentFileShare.type = fileShareInfo.fileType 
     val uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", filePath) 
     intentFileShare.putExtra(Intent.EXTRA_STREAM, uri) 
     fileShareInfo.recipients?.let { 
      intentFileShare.putExtra(Intent.EXTRA_EMAIL, fileShareInfo.recipients) 
     } 
     intentFileShare.putExtra(Intent.EXTRA_SUBJECT, fileShareInfo.shareSubjectText) 
     fileShareInfo.shareExtraText?.let { 
      intentFileShare.putExtra(Intent.EXTRA_TEXT, AppViewUtil.fromHtml(fileShareInfo.shareExtraText!!)) 
     } 
     try { 
      ContextCompat.startActivity(context, Intent.createChooser(intentFileShare, fileShareInfo.shareTitle), null) 
     } catch (e: ActivityNotFoundException) { 
      Toast.makeText(context, context.getString(R.string.sharing_no_app_found), Toast.LENGTH_LONG).show() 
     } 

    } 
} 
Các vấn đề liên quan