2011-05-20 159 views
1

我已經在android 2.2中做了一個應用程序。在其中一項活動中,我應該通過電子郵件發送附件。所以我寫了下面的代碼:下面的代碼有問題嗎?

public void emailButtonClicked(View v) { 
    Intent emailintent = new Intent(Intent.ACTION_SEND); 
    try { 
     HandleDatabase hb = new HandleDatabase(); 
     String data = hb.getAttachmentInfo(id); 
     String details[] = data.split("--"); 
     String formatteddates[] = formatdate(details); 
     details[2] = formatteddates[0]; 
     details[3] = formatteddates[1]; 
     InputStream myInput = this.getAssets().open("exportformat.txt"); 
     BufferedReader inputStream = new BufferedReader(
       new InputStreamReader(myInput)); 

     String outFileName = "/data/data/com.helios.NauticDates/attachment.ics"; 
     File checkfile = new File(outFileName); 
     if (checkfile.exists()) { 
      checkfile.delete(); 
     } 
     for (int i = 0; i < 4; i++) { 
      if (details[i].equals("null")) 
       details[i] = " "; 
     } 
     FileOutputStream myOutput = new FileOutputStream(outFileName); 
     String datafromfile; 
     while ((datafromfile = inputStream.readLine()) != null) { 
      StringBuffer sb = new StringBuffer(datafromfile); 
      if (datafromfile.equals("DTSTART:")) 
       sb.append(details[2]); 
      if (datafromfile.equals("DTEND:")) 
       sb.append(details[3]); 
      if (datafromfile.equals("SUMMARY:")) 
       sb.append(details[0]); 
      if (datafromfile.equals("DESCRIPTION:")) 
       sb.append(details[1]); 
      if (datafromfile.equals("CATEGORIES:")) 
       sb.append(details[4]); 
      datafromfile = sb.toString(); 
      datafromfile += "\n"; 
      byte[] temp = datafromfile.getBytes(); 
      myOutput.write(temp); 
     } 

     // Close the streams 
     myOutput.flush(); 
     myOutput.close(); 
     inputStream.close(); 
     File tempfile = new File(outFileName); 
     emailintent.putExtra(Intent.EXTRA_STREAM, 
       Uri.fromFile(tempfile)); 
     emailintent.setType("plain/text"); 
     startActivity(Intent.createChooser(emailintent, "Send mail...")); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

在上面,當用戶啓動默認的電子郵件客戶端的連接是可見的,但是當用戶發送的電子郵件,附件沒有收到。身體,主體......除了附件以外的所有東西都正在被接收。 這裏發生了什麼問題。

注意

ICS文件是,點擊後直接將事件添加到在Mac電腦中的iCal應用程序的Mac用戶的文件。

失敗的嘗試

我已經嘗試以下,但他們不工作:

  1. 改變了MIME類型爲text/html

  2. 試圖發送像extrastream這:-i.putExtra(Intent.EXTRA_STREAM,Uri.parse(「file://」+ attachmentFilePath));?

在此先感謝您。

+0

這是一樣的http://stackoverflow.com/q/6058245刪除文件之後? – 2011-05-20 12:15:05

+0

是的。它是一樣的,但在這裏它是一個不同的問題......我從早上就一直在這個問題上。 – user743883 2011-05-20 12:16:27

+0

它是如何相同和不同?我不清楚... – 2011-05-20 12:19:14

回答

0
public void emailButtonClicked(View v) { 
    Intent emailintent = new Intent(Intent.ACTION_SEND); 
    try { 

     String data = "kj--hhfjdfh--hjhwjfh--hdjqhf--bjfhejwfh"; 
     String details[] = data.split("--"); 
     String formatteddates[] = new String[5] ; 
     formatteddates[0] = "1"; 
     formatteddates[1] = "2"; 
     formatteddates[2] = "3"; 
     formatteddates[3] = "4"; 
     formatteddates[4] = "5"; 
     details[2] = formatteddates[0]; 
     details[3] = formatteddates[1]; 
     InputStream myInput = this.getAssets().open("file.rtf"); 
     BufferedReader inputStream = new BufferedReader(
       new InputStreamReader(myInput)); 

     String outFileName ="sdcard" 
     + File.separator + "myfile.ics"; 
     File checkfile = new File(outFileName); 
     if (checkfile.exists()) { 
      checkfile.delete(); 
     } 
     for (int i = 0; i < 4; i++) { 
      if (details[i].equals("null")) 
       details[i] = " "; 
     } 
     FileOutputStream myOutput = new FileOutputStream(outFileName); 
     String datafromfile; 
     while ((datafromfile = inputStream.readLine()) != null) { 
      StringBuffer sb = new StringBuffer(datafromfile); 
      if (datafromfile.equals("DTSTART:")) 
       sb.append(details[2]); 
      if (datafromfile.equals("DTEND:")) 
       sb.append(details[3]); 
      if (datafromfile.equals("SUMMARY:")) 
       sb.append(details[0]); 
      if (datafromfile.equals("DESCRIPTION:")) 
       sb.append(details[1]); 
      if (datafromfile.equals("CATEGORIES:")) 
       sb.append(details[4]); 
      datafromfile = sb.toString(); 
      datafromfile += "\n"; 
      byte[] temp = datafromfile.getBytes(); 
      myOutput.write(temp); 
     } 

     // Close the streams 
     myOutput.flush(); 
     myOutput.close(); 
     inputStream.close(); 
     File tempfile = new File(outFileName); 
     emailintent.setType("text/calendar"); 
     emailintent.putExtra(Intent.EXTRA_STREAM, 
       Uri.fromFile(tempfile)); 
     emailintent.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"}); 

     System.out.println("URI file=============>"+ "sdcard" 
       + File.separator + "myfile.ics"); 

     startActivity(Intent.createChooser(emailintent, "Send mail...")); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

這個代碼工作,發送郵件,你可以從SD卡

+0

好的。此代碼有效。那是我正在尋找一個例子。現在我可以修改你的代碼以符合我的意圖......謝謝 – user743883 2011-05-22 08:34:21