为什么此代码不将图像附加到彩信中?
代码非常基本。
share_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Uri image = Uri.parse("android.resource://com.mypac.app/" +
imageToSend);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_STREAM, image);
startActivity(Intent.createChooser(share, "Share with"));
}
});
变量 imageToSend
是 int
- /drawables 目录中图像的 ID。
在共享对话框中,我可以看到消息传递选项。我选择了它,但没有附加图像。有一条消息“无法附加图像”。如果我手动从 SD 卡添加图像,那么它会毫无问题地附加到彩信中。
上面的代码可能有什么问题?
编辑
尝试了其他解决方案:附加 SD 图像。这是代码。
File file = new File(Environment.getExternalStorageDirectory(),
"img.png");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
startActivity(Intent.createChooser(share, "Share with"));
这也不起作用。我仍然收到无法附加文件的消息。 Facebook 应用程序再次完美运行。
The code is pretty basic
share_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Uri image = Uri.parse("android.resource://com.mypac.app/" +
imageToSend);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
share.putExtra(Intent.EXTRA_STREAM, image);
startActivity(Intent.createChooser(share, "Share with"));
}
});
The variable imageToSend
is int
- ID of the image in /drawables directory.
In the share dialog, I can see the Messaging as option. I choose it but no image is attached. There is a message "an image cannot be attached". If I manually add image from the sdcard, then it's being attached to the MMS message with no problems.
What may be the problem with the code above?
EDIT
Tried the other solution: attach image from SD. This is the code.
File file = new File(Environment.getExternalStorageDirectory(),
"img.png");
share.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
startActivity(Intent.createChooser(share, "Share with"));
This does NOT work as well. I still get message that the file cannot be attached. And again Facebook app works flawlessly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
意图被传输到外部应用程序。
android:resource 方案仅在本地有效。
这意味着您必须将图像从资源复制到外部目录并在您的 Intent 中链接这个新文件
The intent is transfered to an external application.
The android:resource scheme is only valid locally.
That means that you have to copy the image from your resources to an external directory and link this new file in your Intent
如何使用 Intent 类中的
FLAG_GRANT_READ_URI_PERMISSION
标志:http://developer.android.com/reference/android/content/Intent.html#FLAG_GRANT_READ_URI_PERMISSION顾名思义,这应该将给定 Uri 的读取权限转移到由那个意图。
How about using the
FLAG_GRANT_READ_URI_PERMISSION
flag from the Intent class : http://developer.android.com/reference/android/content/Intent.html#FLAG_GRANT_READ_URI_PERMISSIONAs the name says, this should transfer Read permission on the given Uri to the activity started by that intent.
你的 Uri.parse() 返回什么?也许为空?
试试这个:
What does your Uri.parse() return? Null maybe?
Try this: