2013-07-27 36 views
1

我正在使用EmailComposer開發Android PhoneGap項目。除了附件外,一切都在工作。我在Eclipse中看不到LogCat中的任何錯誤。在網上瀏覽解決方案以找出文件未附加到電子郵件的原因。我的2.2.2和4.0.4安卓設備工作正常,但沒有附加電子郵件。我正在使用cordova.2.9.0.jar。有人可以點亮一些光線嗎?PhoneGap Android EmailComposer無附件


這裏是觸發EmailComposerWithAttachment插件的.js電子郵件作曲家。

function composeText(){ 
    window.plugins.emailComposer.showEmailComposerWithCallback(
      null, 
      "Look at this photo","Take a look at <b>this<b/>:", 
      ["[email protected]", "[email protected]"], 
      [], 
      [], 
      true, 
      ["file:///mnt/sdcard/Android/data/com.thing.thing/cache/thing/file.wav"] 
       ); 
    } 

這是EmailComposer.java文件

import java.io.File; 
import java.util.ArrayList; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.content.Intent; 
import android.net.Uri; 
import android.text.Html; 

import org.apache.cordova.api.CallbackContext; 
import org.apache.cordova.api.CordovaPlugin; 
import org.apache.cordova.api.LOG; 

public class EmailComposer extends CordovaPlugin { 

    @Override 
    public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { 
     if ("showEmailComposer".equals(action)) { 

      try { 
       JSONObject parameters = args.getJSONObject(0); 
       if (parameters != null) { 
        sendEmail(parameters); 
       } 
      } catch (Exception e) { 

      } 
      callbackContext.success(); 
      return true; 
     } 
     return false; // Returning false results in a "MethodNotFound" error. 
    } 

    private void sendEmail(JSONObject parameters) { 

     final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE); 

     //String callback = parameters.getString("callback"); 

     boolean isHTML = false; 
     try { 
      isHTML = parameters.getBoolean("bIsHTML"); 
     } catch (Exception e) { 
      LOG.e("EmailComposer", "Error handling isHTML param: " + e.toString()); 
     } 

     if (isHTML) { 
      emailIntent.setType("text/html"); 
     } else { 
      emailIntent.setType("text/plain"); 
     } 

     // setting subject 
     try { 
      String subject = parameters.getString("subject"); 
      if (subject != null && subject.length() > 0) { 
       emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject); 
      } 
     } catch (Exception e) { 
      LOG.e("EmailComposer", "Error handling subject param: " + e.toString()); 
     } 

     // setting body 
     try { 
      String body = parameters.getString("body"); 
      if (body != null && body.length() > 0) { 
       if (isHTML) { 
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, Html.fromHtml(body)); 
       } else { 
        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, body); 
       } 
      } 
     } catch (Exception e) { 
      LOG.e("EmailComposer", "Error handling body param: " + e.toString()); 
     } 

     // setting TO recipients 
     try { 
      JSONArray toRecipients = parameters.getJSONArray("toRecipients"); 
      if (toRecipients != null && toRecipients.length() > 0) { 
       String[] to = new String[toRecipients.length()]; 
       for (int i=0; i<toRecipients.length(); i++) { 
        to[i] = toRecipients.getString(i); 
       } 
       emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, to); 
      } 
     } catch (Exception e) { 
      LOG.e("EmailComposer", "Error handling toRecipients param: " + e.toString()); 
     } 

     // setting CC recipients 
     try { 
      JSONArray ccRecipients = parameters.getJSONArray("ccRecipients"); 
      if (ccRecipients != null && ccRecipients.length() > 0) { 
       String[] cc = new String[ccRecipients.length()]; 
       for (int i=0; i<ccRecipients.length(); i++) { 
        cc[i] = ccRecipients.getString(i); 
       } 
       emailIntent.putExtra(android.content.Intent.EXTRA_CC, cc); 
      } 
     } catch (Exception e) { 
      LOG.e("EmailComposer", "Error handling ccRecipients param: " + e.toString()); 
     } 

     // setting BCC recipients 
     try { 
      JSONArray bccRecipients = parameters.getJSONArray("bccRecipients"); 
      if (bccRecipients != null && bccRecipients.length() > 0) { 
       String[] bcc = new String[bccRecipients.length()]; 
       for (int i=0; i<bccRecipients.length(); i++) { 
        bcc[i] = bccRecipients.getString(i); 
       } 
       emailIntent.putExtra(android.content.Intent.EXTRA_BCC, bcc); 
      } 
     } catch (Exception e) { 
      LOG.e("EmailComposer", "Error handling bccRecipients param: " + e.toString()); 
     } 

     // setting attachments 
     try { 
      JSONArray attachments = parameters.getJSONArray("attachments"); 
      if (attachments != null && attachments.length() > 0) { 
       ArrayList<Uri> uris = new ArrayList<Uri>(); 

       //convert from paths to Android friendly Parcelable Uri's 
       for (int i=0; i<attachments.length(); i++) { 
        try { 
         File file = new File(attachments.getString(i)); 
         if (file.exists()) { 
          Uri uri = Uri.fromFile(file); 
          uris.add(uri); 
         } 
        } catch (Exception e) { 
         LOG.e("EmailComposer", "Error adding an attachment: " + e.toString()); 
        } 
       } 
       if (uris.size() > 0) { 
        emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris); 
       } 
      } 
     } catch (Exception e) { 
      LOG.e("EmailComposer", "Error handling attachments param: " + e.toString()); 
     } 

     this.cordova.startActivityForResult(this, emailIntent, 0); 
    } 

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent intent) { 
     // todo handle callback 
     super.onActivityResult(requestCode, resultCode, intent); 
     LOG.e("EmailComposer", "ResultCode: " + resultCode); 
     // IT DOESN'T SEEM TO HANDLE RESULT CODES 
    } 

} 

最後但並非最不重要的EmailComposer.js文件

// window.plugins.emailComposer 

function EmailComposer() { 
    this.resultCallback = null; // Function 
} 

EmailComposer.ComposeResultType = { 
    Cancelled:0, 
    Saved:1, 
    Sent:2, 
    Failed:3, 
    NotSent:4 
} 



// showEmailComposer : all args optional 

EmailComposer.prototype.showEmailComposer = function(subject,body,toRecipients,ccRecipients,bccRecipients,bIsHTML,attachments) { 
    console.log("****************************AVVIATO"); 
    var args = {}; 
    if(toRecipients) 
     args.toRecipients = toRecipients; 
    if(ccRecipients) 
     args.ccRecipients = ccRecipients; 
    if(bccRecipients) 
     args.bccRecipients = bccRecipients; 
    if(subject) 
     args.subject = subject; 
    if(body) 
     args.body = body; 
    if(bIsHTML) 
     args.bIsHTML = bIsHTML; 
    if(attachments) 
     args.attachments = attachments; 

    cordova.exec(null, null, "EmailComposer", "showEmailComposer", [args]); 
} 

EmailComposer.prototype.showEmailComposerWithCallback = function(callback, subject, body, toRecipients, ccRecipients, bccRecipients, isHTML, attachments) { 
    this.resultCallback = callback; 
    this.showEmailComposer.apply(this,[subject,body,toRecipients,ccRecipients,bccRecipients,isHTML,attachments]); 
} 

EmailComposer.prototype._didFinishWithResult = function(res) { 
    this.resultCallback(res); 
} 

cordova.addConstructor(function() { 
    console.log("****************************"); 
         if(!window.plugins) 
         { 
         window.plugins = {}; 
         } 

         // shim to work in 1.5 and 1.6 
         if (!window.Cordova) { 
         window.Cordova = cordova; 
         }; 

         window.plugins.emailComposer = new EmailComposer(); 
         }); 

請注意,我一直在原始EmailComposer github回購位沒有找到任何答案。 Stackoverflow提供了一些見解,但它並沒有專門針對附件指定它。任何幫助是極大的讚賞。

回答

0

我的項目也面臨同樣的問題。我曾見過的問題在於文件的路徑。當我們寫作時,會顯示附件,但接收者從不會獲得附件。

我改變了我的代碼,將文件存儲在SD卡上,然後將該路徑發送給作曲家,它對我來說工作得很好。

所以試試像「file:///sdcard/file.wav」。如果您可以將該文件存儲到此位置,這將工作。

+0

我試過android_asset/www /文件,這沒有奏效。我試圖創建和寫入文件系統,但也沒有工作。所以我超級難住。我也嘗試從手機上的圖庫圖像中抓取圖像以附加,但也未能附加。真的很難倒在這裏。是否有一個工作phongap android emailcomposer應用程序,實際上在網上發送附件? – Josue

+0

嗨,我已經完成,因爲你在這裏提到你能幫我。我有附件probelm http://stackoverflow.com/questions/19111638/multiple-mail-attachment-phonegap – surhidamatya

0

對於Android,您只需從文件路徑中刪除「file:///」。

function composeText(){ 
var filePath = "file:///mnt/sdcard/Android/data/com.thing.thing/cache/thing/file.wav"; 
filePath = filePath.replace(/file:\/\/\//g, ''); 
window.plugins.emailComposer.showEmailComposerWithCallback(
     null, 
     "Look at this photo","Take a look at <b>this<b/>:", 
     ["[email protected]", "[email protected]"], 
     [], 
     [], 
     true, 
     [filePath] 
      ); 
} 
+0

是的謝謝你@Elina,現在它工作正常後刪除「file:///」 –