如何格式化在 android 的 uri.parse() 中使用的路径
我有一个图像位于以下位置(它在我的 Android 工作区资源中),
D:\Android\WorkSpace\myprojectname\res\drawable-hdpi
我使用了以下代码行将此图像附加到电子邮件,但它似乎不起作用,它发送了电子邮件,但它不会有附件。
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM,Uri.parse("android.resource://com.mywebsite.myprojectname/" + R.drawable.image));
这这错了?
I have an image that is in the following location (it's in my android workspace resources),
D:\Android\WorkSpace\myprojectname\res\drawable-hdpi
I have used the following line of code to attach this image to an email but it doesn't seem to be working, it sends the email but it won't have the attachment.
emailIntent.putExtra(android.content.Intent.EXTRA_STREAM,Uri.parse("android.resource://com.mywebsite.myprojectname/" + R.drawable.image));
this this wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
之后,
在您发送电子邮件的地方,执行以下操作,
它肯定会起作用。它完全有效。尝试一下。
After that,
Where u r sending the email, do the following,
It will work for sure. it worked for completely. Try it.
好吧,第一个问题是,即使该 URI 格式正确(我对此表示怀疑),该文件也会位于应用程序的沙箱内,而电子邮件活动(或任何其他活动)无法访问该沙箱。无论如何,您都必须将该文件写入 SD 卡,并让电子邮件程序从那里读取它。您可以使用以下代码输出位图:
然后使用
Uri.fromFile()
从上面定义的文件生成 URI:(资源到文件的代码改编自 此处)
Well, the first problem is that even if that URI format is right (which I doubt), that file would be inside your application's sandbox, which is not accessible to the E-mail activity (or any other activity, for that matter). In any case, you're gonna have to write that file into the SD card and have the email program read it from there. You can output the bitmap using the following code:
Then use
Uri.fromFile()
to generate the URI from the file defined above:(Code for resource to file adapted from here)
其他答案可能也有效(但它们对我不起作用)!我只使用下面的一行代码就可以运行它,这非常简单和容易。谢谢大家。
我认为我的问题是我把包名称写错了,但是使用 getPackageName() 方法可以解决问题!
The other answers might work too (they didn't work for me though)! I got it running using only the one line of code bellow, which is very simple and easy. Thanks everyone.
I think my issue was that I was putting the package name wrong, but using the getPackageName() method that issue is solved!