[英]How to create a Uri from an Android resource with file extension
我正在尝试将res / drawable文件夹中的png文件作为附件发送到我的电子邮件中。 我可以将文件附加并包含在电子邮件中,但缺少.png文件扩展名。 我希望它在文件名中包含扩展名。 有人可以帮忙吗? 这是我的代码:
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();
}
将类型更改为此:
i.setType("image/png");
尽管我认为putParcelableArrayListExtra()
是不必要的。 putExtra()
应该可以完成这项工作。
使用此方法获取文件的扩展名
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.