Java 通过隐式意图共享消息和图像时出错
我希望能够在一个意图中分享信息和图像。这在一定程度上现在起作用。例如,当我在Google keep中共享它时,我可以通过intent获得标题、文本正文和图像。但是,当我尝试通过电子邮件或其他一些消息应用程序发送消息时,我无法发送消息,只会附加文本和标题。我将收到一个错误,提示:无法附加文件Java 通过隐式意图共享消息和图像时出错,java,android,android-studio,Java,Android,Android Studio,我希望能够在一个意图中分享信息和图像。这在一定程度上现在起作用。例如,当我在Google keep中共享它时,我可以通过intent获得标题、文本正文和图像。但是,当我尝试通过电子邮件或其他一些消息应用程序发送消息时,我无法发送消息,只会附加文本和标题。我将收到一个错误,提示:无法附加文件 public void characterShare(String background, String header){ Bundle bundle = getIntent().getExtras(
public void characterShare(String background, String header){
Bundle bundle = getIntent().getExtras();
String name = bundle.getString("name");
Uri imageUri = Uri.parse("android.resource://" + getPackageName()
+ "/drawable/" + name.toLowerCase());
Intent togetherIntent = new Intent();
togetherIntent.setAction(Intent.ACTION_SEND);
togetherIntent.putExtra(Intent.EXTRA_SUBJECT,header); // subject
togetherIntent.putExtra(Intent.EXTRA_STREAM,imageUri); // image
togetherIntent.putExtra(Intent.EXTRA_TEXT, background); // body of the message
togetherIntent.setType("image/jpeg");
togetherIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(togetherIntent, "send"));
}
我希望能够在一个意图中分享信息和图像
ACTION\u SEND
被记录为在单个Intent
中支持EXTRA\u流
或EXTRA\u文本
,而不是两者都支持。一些响应ACTION\u SEND
的应用程序将同时满足这两个条件,但不是全部
但是,当我尝试通过电子邮件或其他一些消息应用程序发送消息时,我无法发送消息,只会附加文本和标题。我将收到一个错误,提示:无法附加文件
public void characterShare(String background, String header){
Bundle bundle = getIntent().getExtras();
String name = bundle.getString("name");
Uri imageUri = Uri.parse("android.resource://" + getPackageName()
+ "/drawable/" + name.toLowerCase());
Intent togetherIntent = new Intent();
togetherIntent.setAction(Intent.ACTION_SEND);
togetherIntent.putExtra(Intent.EXTRA_SUBJECT,header); // subject
togetherIntent.putExtra(Intent.EXTRA_STREAM,imageUri); // image
togetherIntent.putExtra(Intent.EXTRA_TEXT, background); // body of the message
togetherIntent.setType("image/jpeg");
togetherIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(togetherIntent, "send"));
}
您正在使用一个android.resource
Uri
。这有两个问题:
操作发送
和额外流
的文档,该文档规定Uri
应具有内容
方案,并由内容提供者支持
getCacheDir()
中),然后使用FileProvider.getUriForFile()
使其可用。并显示基本概念,尽管我使用的是资产而不是可提取资源,我使用的是ACTION\u VIEW
而不是ACTION\u SEND