0

I´m trying to email an image using javamail. But I get a FileNotFoundException:

05-22 15:05:29.300: W/System.err(29154): javax.mail.MessagingException: IOException while sending message;
05-22 15:05:29.300: W/System.err(29154):   nested exception is:
05-22 15:05:29.300: W/System.err(29154):    java.io.FileNotFoundException: /content:/media/external/images/media/35367: open failed: ENOENT (No such file or directory)

I think this is the part of the method which causes the exception:

        System.out.println(params[1]);
        File filename=new File(params[1]); // params[1]=content://media/external/images/media/35367
        DataSource source = new FileDataSource(filename);//uses aditional
        messageBodyPart.setDataHandler(new DataHandler(source));

But if I understood the API right an absolute uri isn´t a problem for a File-object. And I think it isn´t possible that the file doesn´t exist, because right before this method I´m setting an ImageView by this uri. Could this be the problem? Something like that android automatically locks this file?

I also set Read and Write rights at the AndroidManifest:

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Just comment if you need further information.

EDIT: I looked up the path to the image I´m trying to send and it differs completely from the path where it is saved...

Sebastian Walla
  • 1,104
  • 1
  • 9
  • 23
  • Have you tried using javamail to send a message without sending an attachment? it's possible it's not related to the file you're trying to attach, but something else. – Stealth Rabbi May 22 '14 at 13:36
  • @StealthRabbi Sending a message without an attachment worked fine... – Sebastian Walla May 22 '14 at 13:42
  • Did you ever find a solution to this? I am facing this very issue - attaching images from dropbox (/storage) works fine but others, including native apps, fail - even with external file permissions on. – RelicScoth Nov 07 '14 at 19:54
  • 1
    @RelicScoth yes! `/**Returns the real path from a relative URI*/ private String getRealPathFromURI(Uri contentUri) { String[] proj = { MediaStore.Images.Media.DATA }; CursorLoader cursorLoader = new CursorLoader(this,contentUri, proj, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); }` worked for me but it´s deprecated since api level 11 – Sebastian Walla Nov 09 '14 at 19:43

3 Answers3

2

I´m trying to email an image using javamail. But I get a FileNotFoundException

That is because what you have is not a file. It is an entry in a ContentProvider.

Get an InputStream on the content using openInputStream() on ContentResolver. Then, use that with a ByteArrayDataSource instead of a FileDataSource. In theory, that should work, and at least should get you closer to something that works.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • So how do I get a ContentResolver because getContentResolver() can´t be called caused by using it in an AsyncTask – Sebastian Walla May 22 '14 at 14:36
  • @burnaut: Implement a constructor on your `AsyncTask` that takes in a `ContentResolver` and holds onto it in a data member. Then, use that `ContentResolver`. – CommonsWare May 22 '14 at 14:52
0

This

/content:/media/external/images/media/35367

is not valid path on your device. If you want absolute path you should have

/media/external/images/media/35367
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • I also tried this one, getting it by using uri.getPath().toString() but I keep getting the same exception and i used uri.isAbsolute() to verify that the uri I´m using is absolute. – Sebastian Walla May 22 '14 at 13:39
0

Haven't you left the extension of the image out of the uri?

For instance, /content:/media/external/images/media/35367.jpg for a JPG image?

Stu Whyte
  • 758
  • 5
  • 19