[英]How to create a Uri from an Android resource with file extension
I am trying to send a png file in my res/drawable folder as an attachment in my email. 我正在尝试将res / drawable文件夹中的png文件作为附件发送到我的电子邮件中。 I can get the file to attach and be included in the email, but it is missing the .png file extension.
我可以将文件附加并包含在电子邮件中,但缺少.png文件扩展名。 I want it to include the extension in the file name.
我希望它在文件名中包含扩展名。 Can someone help?
有人可以帮忙吗? Here is my code:
这是我的代码:
Intent i = new Intent(Intent.ACTION_SEND_MULTIPLE);
i.setType("message/rfc822");
i.putExtra(Intent.EXTRA_SUBJECT, "Subject");
i.putExtra(Intent.EXTRA_TEXT , "Text");
ArrayList<Uri> uris = new ArrayList<Uri>();
Uri path = Uri.parse("android.resource://" + getPackageName() + "/" + R.drawable.png_image);
uris.add(newPath);
i.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
try {
startActivity(Intent.createChooser(i, "Send mail..."));
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(ShareActivity.this, "There are no email clients installed.", Toast.LENGTH_SHORT).show();
}
Change the type to this: 将类型更改为此:
i.setType("image/png");
Although I believe putParcelableArrayListExtra()
is not necessary. 尽管我认为
putParcelableArrayListExtra()
是不必要的。 putExtra()
should do the job. putExtra()
应该可以完成这项工作。
Use this method to get the extension of the file 使用此方法获取文件的扩展名
public static String getMimeType(Context context, Uri uri) {
String extension;
//Check uri format to avoid null
if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
//If scheme is a content
final MimeTypeMap mime = MimeTypeMap.getSingleton();
extension = mime.getExtensionFromMimeType(context.getContentResolver().getType(uri));
} else {
//If scheme is a File
//This will replace white spaces with %20 and also other special characters. This will avoid returning null values on file name with spaces and special characters.
extension = MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(new File(uri.getPath())).toString());
}
return extension;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.