简体   繁体   English

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

[英]Error with sharing message and image through implicit intent

I want to be able to share the message and the image in one intent.我希望能够在一个意图中共享消息和图像。 This works right now partially.这现在部分有效。 For example, when I share it in Google keep, I am able to get the header, text body and the image through intent.例如,当我在 Google Keep 中分享它时,我可以通过 Intent 获取 header、文本正文和图像。 However, when I try to send it through email or some other messaging app, I am unable to send the message, only the text and header get attached.但是,当我尝试通过 email 或其他一些消息传递应用程序发送它时,我无法发送消息,只有文本和 header 被附加。 I'll get an error saying: unable to attach file.我会收到一条错误消息:无法附加文件。

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"));
}

I want to be able to share the message and the image in one intent我希望能够在一个意图中共享消息和图像

ACTION_SEND is documented to support either EXTRA_STREAM or EXTRA_TEXT in a single Intent , not both. ACTION_SEND被记录为在单个Intent中支持EXTRA_STREAMEXTRA_TEXT 而不是同时支持两者。 Some apps responding to ACTION_SEND will honor both, but not all.一些响应ACTION_SEND的应用会同时支持这两者,但不是全部。

However, when I try to send it through email or some other messaging app, I am unable to send the message, only the text and header get attached.但是,当我尝试通过 email 或其他一些消息传递应用程序发送它时,我无法发送消息,只有文本和 header 被附加。 I'll get an error saying: unable to attach file.我会收到一条错误消息:无法附加文件。

You are using an android.resource Uri .您正在使用android.resource Uri That has two problems:这有两个问题:

  1. It is very obscure, and not all apps will support it它非常晦涩难懂,并非所有应用程序都会支持它

  2. It goes against the documentation for ACTION_SEND and EXTRA_STREAM , which state that the Uri should have a content scheme and be backed by a ContentProvider它违反了ACTION_SENDEXTRA_STREAM的文档,其中 state 认为Uri应该具有content方案并由ContentProvider支持

The simplest solution is for you to copy your resource to some file on the filesystem (eg, in getCacheDir() ), then make it available using FileProvider.getUriForFile() .最简单的解决方案是将资源复制到文件系统上的某个文件(例如,在getCacheDir()中),然后使用FileProvider.getUriForFile()使其可用。 This sample Java project and its Kotlin counterpart show the basic concept, though I am using an asset instead of drawable resource, and I am using ACTION_VIEW instead of ACTION_SEND . 这个示例 Java 项目及其 Kotlin 对应项目显示了基本概念,尽管我使用的是资产而不是可绘制资源,并且我使用的是ACTION_VIEW而不是ACTION_SEND

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM