2015-05-07 70 views
1

我正在嘗試創建pdf並通過電子郵件發送。現在我已經成功完成了pdf一代。當我嘗試將PDF附加到郵件併發送時,我收到錯誤。無法在pdf中附加pdf到郵件中

我想是這樣的:

private void sendMail(Uri URI) { 
     try { 
      // Uri URI = null; 

      String email = emailFromDB; 

      String subject = "Report For the day " + getDateTime(); 

      String message = "Report For the day " + getDateTime(); 

      final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
      emailIntent.setType("application/pdf"); 
      emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] { email }); 
      emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,subject); 

      if (URI != null) { 
       emailIntent.putExtra(Intent.EXTRA_STREAM, URI); 
      } 

      emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message); 
      emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      this.startActivity(Intent.createChooser(emailIntent,"Sending email...")); 

     } catch (Throwable t) { 

      Toast.makeText(this, 

      "Request failed try again: " + t.toString(), 

      Toast.LENGTH_LONG).show(); 

     } 

    } 

,當我嘗試發送我得到這個erroe。

05-07 22:51:08.616: E/Gmail(326): java.io.FileNotFoundException: No content provider: /mnt/sdcard/MyMenu/2015-05-07 22-50-39-Report.pdf 
05-07 22:51:08.616: E/Gmail(326): at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:604) 
05-07 22:51:08.616: E/Gmail(326): at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:536) 
05-07 22:51:08.616: E/Gmail(326): at android.content.ContentResolver.openFileDescriptor(ContentResolver.java:449) 
05-07 22:51:08.616: E/Gmail(326): at com.android.mail.compose.f.b(SourceFile:2817) 
05-07 22:51:08.616: E/Gmail(326): at com.android.mail.compose.f.a(SourceFile:3484) 
05-07 22:51:08.616: E/Gmail(326): at com.google.android.gm.ComposeActivityGmail.a(SourceFile:560) 
05-07 22:51:08.616: E/Gmail(326): at com.android.mail.compose.f.a(SourceFile:3122) 
05-07 22:51:08.616: E/Gmail(326): at com.android.mail.compose.f.w(SourceFile:2559) 
05-07 22:51:08.616: E/Gmail(326): at com.android.mail.compose.f.onOptionsItemSelected(SourceFile:2505) 
05-07 22:51:08.616: E/Gmail(326): at com.google.android.gm.ComposeActivityGmail.onOptionsItemSelected(SourceFile:342) 
05-07 22:51:08.616: E/Gmail(326): at android.app.Activity.onMenuItemSelected(Activity.java:2502) 
05-07 22:51:08.616: E/Gmail(326): at android.support.v4.app.l.onMenuItemSelected(SourceFile:350) 
05-07 22:51:08.616: E/Gmail(326): at android.support.v7.app.g.onMenuItemSelected(SourceFile:155) 
05-07 22:51:08.616: E/Gmail(326): at android.support.v7.app.i.a(SourceFile:74) 
05-07 22:51:08.616: E/Gmail(326): at android.support.v7.app.ActionBarActivityDelegateBase.a(SourceFile:556) 
05-07 22:51:08.616: E/Gmail(326): at android.support.v7.internal.view.menu.i.a(SourceFile:802) 
05-07 22:51:08.616: E/Gmail(326): at android.support.v7.internal.view.menu.m.b(SourceFile:153) 
05-07 22:51:08.616: E/Gmail(326): at android.support.v7.internal.view.menu.i.a(SourceFile:949) 
05-07 22:51:08.616: E/Gmail(326): at android.support.v7.internal.view.menu.i.a(SourceFile:939) 
05-07 22:51:08.616: E/Gmail(326): at android.support.v7.widget.ActionMenuView.a(SourceFile:596) 
05-07 22:51:08.616: E/Gmail(326): at android.support.v7.internal.view.menu.ActionMenuItemView.onClick(SourceFile:145) 
05-07 22:51:08.616: E/Gmail(326): at android.view.View.performClick(View.java:3574) 
05-07 22:51:08.616: E/Gmail(326): at android.view.View$PerformClick.run(View.java:14293) 
05-07 22:51:08.616: E/Gmail(326): at android.os.Handler.handleCallback(Handler.java:605) 
05-07 22:51:08.616: E/Gmail(326): at android.os.Handler.dispatchMessage(Handler.java:92) 
05-07 22:51:08.616: E/Gmail(326): at android.os.Looper.loop(Looper.java:137) 
05-07 22:51:08.616: E/Gmail(326): at android.app.ActivityThread.main(ActivityThread.java:4448) 
05-07 22:51:08.616: E/Gmail(326): at java.lang.reflect.Method.invokeNative(Native Method) 
05-07 22:51:08.616: E/Gmail(326): at java.lang.reflect.Method.invoke(Method.java:511) 
05-07 22:51:08.616: E/Gmail(326): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:823) 
05-07 22:51:08.616: E/Gmail(326): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:590) 
05-07 22:51:08.616: E/Gmail(326): at dalvik.system.NativeStart.main(Native Method) 

這裏我得到了FileNotFoundException erroe。但我在這個位置有pdf。(/mnt/sdcard/MyMenu/2015-05-07 22-50-39-Report.pdf

請讓我知道我犯的錯誤。

+1

「URI」似乎格式不正確。我不知道你從哪裏得到它。它沒有一個計劃。如果你從'File'創建它,使用'Uri.fromFile()'創建'Uri'值。另外請注意,您不能同時使用'EXTRA_TEXT'和'EXTRA_STREAM'以及'ACTION_SEND'。選擇一個並使用它,根據您的選擇在「Intent」上設置MIME類型。 – CommonsWare

+0

嗨@CommonsWare ..嗨@CommonsWare ..謝謝你的提示..我會檢查,並讓你現在知道.. –

+0

超.. :-)現在我可以發送郵件附件..謝謝@CommonsWare ..請張貼您的答案..! ! –

回答

1

所以芬利我得到的溶液(@CommonsWare給出提示)..

sendMail(Uri.fromFile(emailFilePath)); 

發送電子郵件方法: -

private void sendMail(Uri URI) { 
     try { 

      String email = emailFromDB; 

      String subject = "Report For the day " + getDateTime(); 

      String message = "Report For the day " + getDateTime(); 

      final Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND); 
      emailIntent.setType("application/pdf"); 
      emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,new String[] { email }); 
      emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,subject); 

      if (URI != null) { 
       emailIntent.putExtra(Intent.EXTRA_STREAM, URI); 
      } 

      emailIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
      this.startActivity(Intent.createChooser(emailIntent,"Sending email...")); 

     } catch (Throwable t) { 

      Toast.makeText(this, 

      "Request failed try again: " + t.toString(), 

      Toast.LENGTH_LONG).show(); 

     } 

    } 

在這裏,我已刪除emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, message);並獲得URI像Uri.fromFile(emailFilePath)。這解決了我的問題。感謝@CommonnsWare .. !!!