0

ı am trying to send an email with attachement so.I have one image file located in downloads .But when I choose image then applicaiton crashs.I tried to debug and set a breakpoint on onActivityResult saw Result_code returns -1 .So ı have no idea what am i doing wrong .

   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);

   }

this the output

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
user3651582
  • 45
  • 1
  • 4

1 Answers1

1

I'm thinking that this is the problem :

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

I think attachmentFile is null. So something goes wrong there.

Maybe do it like this to make sure:

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

The first parameter of this Log is your tag. The tag is something you see to group messages in your logcat. So System.out.prinln will have this as tag for example: com.example.app

Kevin van Mierlo
  • 9,554
  • 5
  • 44
  • 76
  • You are right path is null.Android file sysytem structure is so complicated.I have just download an image from internet via emulator and trying to select that image .But I have no idea what is the right part.What sould i do. – user3651582 Jun 06 '14 at 08:53
  • @user3651582 Can you update your logcat output? Because I think there is a different error message now – Kevin van Mierlo Jun 06 '14 at 09:00