0

I am Working email attachment .I am facing one problem while attachment .Problem Is that i want to sent a mail with attachment .I have one file on this path sdcard0 then fgg then hh.html.When I debug on this File file = new File(attachments.getString(i)); it show file:/storage/sdcard0/fgg/hh.html But After this it not go to if condition why ?

File file = new File(attachments.getString(i));
                        if (file.exists()) {
                            Uri uri = Uri.fromFile(file);
                            uris.add(uri);
                        }

Here is my hole code

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());
    }
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
Neetu sharma
  • 148
  • 3
  • 15
  • What kind of data is there in the attachements array of your JSON? file:/storage/sdcard0/fgg/file.html or things more like /storage/sdcard0/fgg/hh.html? – ol_v_er Jul 31 '13 at 08:32

3 Answers3

1

Uri to file will need triple slashes :

file:///storage/sdcard0/fgg/hh.html

As you can see here. But i twill probably not work, so try to remove the "file:" part of your string :

File file = new File(attachments.getString(i).replace("file:","");
Community
  • 1
  • 1
NitroG42
  • 5,336
  • 2
  • 28
  • 32
1

in a URI, you have different parts.

First the scheme:

  • http://
  • file://
  • ftp://

Then the path you want to access:

  • myfile.txt
  • videos/myvideo.avi
  • /storage/sdcard0/fgg/hh.html

So your complete URI should be:

file:///storage/sdcard0/fgg/hh.html

More information here:

http://developer.android.com/reference/java/net/URI.html

You can then build your File and check if it exist with this snippet

Uri.Builder builder = new Uri.Builder();
builder.scheme("file");
builder.path(myFilePath);
Uri uri = builder.build();
File file = new File(uri.getPath());
if (file.exists()) {
    uris.add(uri);
    // do whatever you want
}

EDIT:

If your JSON send you complete URI path, use this code instead:

Uri uri = Uri.parse(attachments.getString(i));
File file = new File(uri.getPath());
if (file.exists()) {
    uris.add(uri);
    // do whatever you want
}
ol_v_er
  • 27,094
  • 6
  • 48
  • 61
0

Try two forward slashes after :

file://storage/sdcard0/fgg/hh.html

edit:

should be 3 forward slashes

josephus
  • 8,284
  • 1
  • 37
  • 57