问题
I am writing an android app which has a feature that will send an email with an attachment of a specific document. This is working but the attachment, which is called "peroneal.pdf" when I attach it to the email, (as an intent, which I'm sure is where the issue is) becomes "2131034113.pdf" when the email is received. How do I change it so that the received document has the original name? Does it have to do with naming the intent? If so, how do I do this? Thanks in advance for any help, I have attached the code snippet:
Intent i = new Intent(Intent.ACTION_SEND);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_EMAIL , new String[]{value.toString()});
i.putExtra(Intent.EXTRA_SUBJECT, "Tendon Email");
i.putExtra(Intent.EXTRA_TEXT , "The info is attached, just hit send.");
String rawFolderPath = "android.resource://" + getPackageName() + "/" + R.raw.peroneal;
Uri emailUri = Uri.parse(rawFolderPath);
i.putExtra(Intent.EXTRA_STREAM, emailUri);
i.setType("application/pdf");
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(PTSlideShow.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
回答1:
This is working but the attachment, which is called "peroneal.pdf"
No, it is not called "peroneal.pdf", at least not on the device.
You may have a file named of peroneal.pdf
on your local filesystem. That is largely lost when you package it as a resource, as you apparently have.
What the other process will see is android.resource://.../2131034113
, where 2131034113
is the decimal value of R.raw.peroneal
(and ...
is your app's package name).
How do I change it so that the received document has the original name?
Well, you will increase your odds by using a Uri
that actually has peroneal.pdf
in it. For example, you could copy your raw resource out to external storage and use a File
-based Uri
instead. Or, serve up the attachment via an openFile()
-based ContentProvider
, where you support a Uri
that ends in peroneal.pdf
.
However, bear in mind that you are asking other apps to send email on your behalf. How those emails get created and packaged is up to the authors of those other email apps. There is no guarantee, at all, that your attachment will be named based on the last segment of the Uri
. Probably there are many email apps that will take this approach, but I would not be the least bit surprised if there are some that will not.
来源:https://stackoverflow.com/questions/13298758/email-attachment-send-by-app-has-different-name-than-file-that-is-sent