2014-02-04 2 views
0
public void onClick(View arg0) { 

     String fname1 = text_fname.getText().toString(); 
     String fname2 = edit_fname.getText().toString(); 

     String lname1 = text_lname.getText().toString(); 
     String lname2 = edit_lname.getText().toString();  

     String space = "\t"; 
     String newLine = "\n"; 

     File file = null; 
     FileOutputStream fos = null; 

     try { 
      file = getActivity().getFilesDir(); 
      fos = getActivity().openFileOutput("test.xls", Context.MODE_PRIVATE); 
      fos.write(fname1.getBytes()); 
      fos.write(space.getBytes()); 
      fos.write(fname2.getBytes()); 
      fos.write(newLine.getBytes()); 
      fos.write(lname1.getBytes()); 
      fos.write(space.getBytes()); 
      fos.write(lname2.getBytes()); 
      fos.close(); 

     } catch (FileNotFoundException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } finally { 
      try { 
       fos.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 

     if (fos!=null) { 
      try { 
       fos.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
     Toast.makeText(getActivity(), "File saved in " + file, Toast.LENGTH_LONG).show(); 

     Intent i = new Intent(Intent.ACTION_SEND); 
     i.setType("message/rfc822"); 
     i.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"}); 
     i.putExtra(Intent.EXTRA_SUBJECT, "subject of email"); 
     i.putExtra(Intent.EXTRA_TEXT, "body of email"); 
     startActivity(Intent.createChooser(i, "Send mail...")); 
    } 

На нажатии кнопки, я создаю файл «test.xls» со строками внутри и также называю это:Вложить файл по электронной почте

enter image description here

Вот результат после нажатия Gmail:

enter image description here

Мои вопросы, как я могу прикрепить файл "test.xls" в моей электронной почте? Поэтому я могу отправить его кому бы то ни было получателю.

+0

посмотрите: http://developer.android.com/reference/android/content/Intent.html#EXTRA_STREAM – peshkira

ответ

1
File file = new File(Environment.getExternalStorageState()+"/folderName/test.xls"); 
     Uri path = Uri.fromFile(file); 
     Intent intent = new Intent(android.content.Intent.ACTION_SEND); 
     intent.setType("application/octet-stream"); 
     intent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject); 
     String to[] = { email }; 
     intent.putExtra(Intent.EXTRA_EMAIL, to); 
     intent.putExtra(Intent.EXTRA_TEXT, message); 
     intent.putExtra(Intent.EXTRA_STREAM, path); 
     startActivityForResult(Intent.createChooser(intent, "Send mail..."), 
       1222); 
+0

Что вы подразумеваете под/folderName /? Вы имеете в виду данные/data/packagename/files? – luwionline

+0

нет пути к файлу –

+0

В нем говорится: «Не удается добавить это приложение» – luwionline

0

Используйте приведенный ниже код.

Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
     emailIntent.setType("application/octet-stream"); 
     emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] { 
       "mail-id" }); 
     emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject); 
     emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message); 
     Uri uri = Uri.fromFile(new File(Environment 
       .getExternalStorageDirectory(), "/folder_name/file_name")); 
     emailIntent.putExtra(Intent.EXTRA_STREAM, uri); 
     emailIntent.setType("text/plain"); 
     startActivity(emailIntent); 
+0

В нем говорится: «Не удается добавить это приложение» – luwionline

1

Добавить следующую строку в ваш Intent.

String PATH="Full path of the File that you want to send" ; 
i.putExtra(Intent.EXTRA_STREAM, Uri.parse(PATH)); 
+0

Как узнать путь? – luwionline

+0

Это то, что показывает мой тост? – luwionline

+0

@ user3173245: если 'file' - это имя' File', которое вы хотите присоединить, тогда 'file.getAbsolutePath()' вернет путь к файлу. –

0

Попробуйте этот подход, в проекте создание EmailActivity.java класса и вставить этот код ..

package com.app.yourpackegename; 

import java.io.File; 
import android.app.Activity; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.os.Environment; 

public class EmailActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 

     // to send data.xls stored in Root of SDCARD 
     // Environment.getExternalStorageState() returns location of sdcard 

     String fullFilePath = Environment.getExternalStorageState()+ "/data.xls"; 
     String email = "[email protected],com"; 
     String subject = "email subject"; 
     String message = "custome message string"; 

     File file = new File(fullFilePath); 
     Uri path = Uri.fromFile(file); 
     Intent intent = new Intent(android.content.Intent.ACTION_SEND); 
     intent.setType("application/octet-stream"); 
     intent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject); 
     String to[] = { email }; 
     intent.putExtra(Intent.EXTRA_EMAIL, to); 
     intent.putExtra(Intent.EXTRA_TEXT, message); 
     intent.putExtra(Intent.EXTRA_STREAM, path); 

     int requestCode = 1000; 

     startActivityForResult(
       Intent.createChooser(intent, "Send mail via..."), requestCode); 
    } 
} 

Надеется, что это помогает вам. дайте мне знать :)

Смежные вопросы