Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 通过隐式意图共享消息和图像时出错_Java_Android_Android Studio - Fatal编程技术网

Java 通过隐式意图共享消息和图像时出错

Java 通过隐式意图共享消息和图像时出错,java,android,android-studio,Java,Android,Android Studio,我希望能够在一个意图中分享信息和图像。这在一定程度上现在起作用。例如,当我在Google keep中共享它时,我可以通过intent获得标题、文本正文和图像。但是,当我尝试通过电子邮件或其他一些消息应用程序发送消息时,我无法发送消息,只会附加文本和标题。我将收到一个错误,提示:无法附加文件 public void characterShare(String background, String header){ Bundle bundle = getIntent().getExtras(

我希望能够在一个意图中分享信息和图像。这在一定程度上现在起作用。例如,当我在Google keep中共享它时,我可以通过intent获得标题、文本正文和图像。但是,当我尝试通过电子邮件或其他一些消息应用程序发送消息时,我无法发送消息,只会附加文本和标题。我将收到一个错误,提示:无法附加文件

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