问题描述:

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

}

网友答案:

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:","");
网友答案:

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
}
网友答案:

Try two forward slashes after :

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

edit:

should be 3 forward slashes

相关阅读:
Top