2012-04-02 3 views
36

내 메일에 .vcf 파일을 첨부하고 메일을 보내려합니다. 그러나 메일 첨부 파일없이 주소에서 수신됩니다. 나는 아래의 코드를 사용하지만이 코드와 내가 어디 잘못된지 모르겠다.안드로이드에 첨부 파일이있는 이메일을 보내는 방법

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

답변

69

공식 Android site이 나를 위해 일에 메일

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

한번보고 ... HTTP : //stackoverflow.com/questions/12798001/android-how-to-send-multiple-contacts-are-attached-in-single-vcf-file- and-send – NagarjunaReddy

+3

"하드 코딩 된"경로는 장치에 따라 변경 될 수 있으므로 사용하면 안됩니다. 파일 위치의 정의를 다음과 같이 변경하는 것이 좋습니다. Filelocation = new File (Environment.getExternalStorageDirectory(). getAbsolutePath(), filename); 다음을 정의하십시오. Uri 경로 = Uri.fromFile (파일 위치); 귀하의 의도에 넣으십시오 : emailIntent .putExtra (Intent.EXTRA_STREAM, path); –

+1

emailIntent.putExtra (Intent.EXTRA_STREAM, filelocation)는 파일을 첨부하지 않지만 emailIntent.putExtra (Intent.EXTRA_STREAM, Uri.parse ("file : //"+ filelocation))를 사용하면 Phillip이 정상적으로 작동합니다. – andytrombone

4

에게 예를 전송하기 위해 아래의 코드를 사용합니다. 아가 왈의 대답

+0

제 경우에는 첨부 파일없이 메일 클라이언트로 이동합니다. 표시된 토스트는 "cant send empty file"입니다. 내 파일은'/ data/data/com.example.app/files/temp.txt'에 저장되고'emailIntent.putExtra (Intent.EXTRA_STREAM, Uri.parse ("content : /"+ filePath)); // filePath is /data/com.example.app/files/temp.txt – kAmol

+0

파일이 앱의 캐시 디렉토리에 있고 앱에서만 해당 디렉토리에서 읽을 수 있기 때문에 파일을 보낼 수 없습니다. Environment.getExternalStorageDirectory()와 같은 다른 디렉토리를 사용해야합니다. – Borzh

+0

사용 된 Environment.getExternalStorageDirectory(), 해당 경로가 유효하고 해당 파일의 데이터가 양호한지 확인했지만 .... 여전히 동일한 오류 메시지 (?)가 표시됩니다. – CESDewar

4

Folder_name으로 이루어으로

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

을 추가 할 필요 무엇 모든 휴대 전화의 사용자의 내부 저장 장치에있는 파일의 이름입니다. (실제로 EXTERNAL_STORAGE). file_name은 보내려는 파일의 이름입니다.

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 doesnt support attachment. 공급자를 사용하여 내 대답을 추가하여 파일 정보를 읽었습니다. 그것의 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() 
     } 

    } 
}