2014-06-06 52 views
0

我試圖發送帶有附件的電子郵件so.I有一個圖像文件位於下載。但是當我選擇圖像然後applicationaiton crashs.I試圖調試和設置一個斷點onActivityResult看到Result_code返回-1.So我不知道我做錯了什麼。不能發送帶有圖像附件的郵件

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
      if (requestCode == PICK_FROM_GALLERY && resultCode == RESULT_OK) { 
       /** 
        * Get Path 
        */ 
       Uri selectedImage = data.getData(); 
       String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

       Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null); 
       cursor.moveToFirst(); 
       columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
       attachmentFile = cursor.getString(columnIndex); 
       Log.e("Attachment Path:", attachmentFile); 
       URI = Uri.parse("file://" + attachmentFile); 
       cursor.close(); 
      } 
    } 

    @Override 
    public void onClick(View v) { 

      if (v == btnAttachment) { 
       openGallery(); 

      } 
      if (v == btnSend) { 
       try { 
         email = editTextEmail.getText().toString(); 
         subject = editTextSubject.getText().toString(); 
         message = editTextMessage.getText().toString(); 

         final Intent emailIntent = new Intent(
            android.content.Intent.ACTION_SEND); 
         emailIntent.setType("plain/text"); 
         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); 
         this.startActivity(Intent.createChooser(emailIntent, 
            "Sending email...")); 

       } catch (Throwable t) { 
         Toast.makeText(this, 
            "Request failed try again: " + t.toString(), 
            Toast.LENGTH_LONG).show(); 
       } 
      } 

    } 

    public void openGallery() { 
      Intent intent = new Intent(); 
      intent.setType("image/*"); 
      intent.setAction(Intent.ACTION_GET_CONTENT); 
      intent.putExtra("return-data", true); 
      startActivityForResult(
         Intent.createChooser(intent, "Complete action using"), 
         PICK_FROM_GALLERY); 

    } 

這個輸出

07-07 04:01:49.080: E/AndroidRuntime(1700): FATAL EXCEPTION: main 
07-07 04:01:49.080: E/AndroidRuntime(1700): Process: com.example.mailapp, PID: 1700 
07-07 04:01:49.080: E/AndroidRuntime(1700): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=101, 
result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:23 flg=0x1 }} to activity 
{com.example.mailapp/com.example.mailapp.MainActivity}: java.lang.NullPointerException: println needs a message 
07-07 04:01:49.080: E/AndroidRuntime(1700):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3346) 
07-07 04:01:49.080: E/AndroidRuntime(1700):  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3389) 
07-07 04:01:49.080: E/AndroidRuntime(1700):  at android.app.ActivityThread.access$1200(ActivityThread.java:135) 
07-07 04:01:49.080: E/AndroidRuntime(1700):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1445) 
07-07 04:01:49.080: E/AndroidRuntime(1700):  at android.os.Handler.dispatchMessage(Handler.java:102) 
07-07 04:01:49.080: E/AndroidRuntime(1700):  at android.os.Looper.loop(Looper.java:137) 
07-07 04:01:49.080: E/AndroidRuntime(1700):  at android.app.ActivityThread.main(ActivityThread.java:4998) 
07-07 04:01:49.080: E/AndroidRuntime(1700):  at java.lang.reflect.Method.invokeNative(Native Method) 
07-07 04:01:49.080: E/AndroidRuntime(1700):  at java.lang.reflect.Method.invoke(Method.java:515) 
07-07 04:01:49.080: E/AndroidRuntime(1700):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777) 
07-07 04:01:49.080: E/AndroidRuntime(1700):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593) 
07-07 04:01:49.080: E/AndroidRuntime(1700):  at dalvik.system.NativeStart.main(Native Method) 
07-07 04:01:49.080: E/AndroidRuntime(1700): Caused by: java.lang.NullPointerException: println needs a message 
07-07 04:01:49.080: E/AndroidRuntime(1700):  at android.util.Log.println_native(Native Method) 
07-07 04:01:49.080: E/AndroidRuntime(1700):  at android.util.Log.e(Log.java:232) 
07-07 04:01:49.080: E/AndroidRuntime(1700):  at com.example.mailapp.MainActivity.onActivityResult(MainActivity.java:56) 
07-07 04:01:49.080: E/AndroidRuntime(1700):  at android.app.Activity.dispatchActivityResult(Activity.java:5435) 
07-07 04:01:49.080: E/AndroidRuntime(1700):  at android.app.ActivityThread.deliverResults(ActivityThread.java:3342) 
07-07 04:01:49.080: E/AndroidRuntime(1700):  ... 11 more 
+0

http://stackoverflow.com/questions/1247983/problem-sending-an-email-with-an-attachment-programmatically –

回答

1

我在想,這就是問題所在:

Log.e("Attachment Path:", attachmentFile);

我覺得attachmentFilenull。所以在那裏出了問題。

也許像這樣做,以確保:

Log.e("MyApp", "Attachment Path: " + attachmentFile);

該日誌的第一個參數是你的標籤。標籤是你看到的在你的logcat中分組消息的東西。所以System.out.prinln將以此標籤爲例:com.example.app

+0

你是對的路徑null.Android文件系統結構非常複雜。我只是通過模擬器從互聯網上下載圖像並嘗試選擇該圖像。但我不知道什麼是正確的部分。我該怎麼辦。 – user3651582

+0

@ user3651582你可以更新你的logcat輸出嗎?因爲我認爲現在有一個不同的錯誤信息 –

相關問題