通过Intent无法共享图像和链接

时间:2019-07-09 12:16:46

标签: android android-intent uri share

通过在网络上搜索,我发现邀请其他人下载我的应用可以通过多种方式共享它。

我尝试了这段有效的代码,向用户显示了应用选择器窗格。

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "My subject");
intent.putExtra(Intent.EXTRA_TITLE, "My subject");
intent.putExtra(Intent.EXTRA_TEXT, "https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID);
startActivity(Intent.createChooser(intent, "Share App"));

当用户选择与WhatsApp,Telegram,SMS,电子邮件等共享消息时,为了正确显示消息,我必须在意图中包括哪些信息?

例如,此代码将在Telegram中显示预览(带有突出显示的链接和预览图像),而不在WhatsApp中显示(仅显示要作为消息发送的纯文本):为什么?

我也尝试了此代码,但它适用于电报,但不适用于whatsapp(它发送的消息仅带有无法打开的带有“无标题”文本的附件):

Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TITLE, "title test");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "subject test");
String shareMessage= "message test\n\n";
shareMessage = shareMessage + "https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID +"\n\n";
shareIntent.putExtra(Intent.EXTRA_TEXT, shareMessage);
shareIntent.putExtra(Intent.EXTRA_HTML_TEXT, "HTML " + shareMessage);
Uri imageUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://"
        + res.getResourcePackageName(R.drawable.testjpg) + '/'
        + res.getResourceTypeName(R.drawable.testjpg) + '/'
        + res.getResourceEntryName(R.drawable.testjpg));
Toast.makeText(this, imageUri.toString(), Toast.LENGTH_LONG).show();
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/jpg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, getString(R.string.share)));

如何使其适用于WhatsApp,Telegram,FB,电子邮件和其他仅文本的短信(如SMS)?

3 个答案:

答案 0 :(得分:4)

  

当用户选择与WhatsApp,Telegram,SMS,电子邮件等共享消息时,为了正确显示消息,我必须在意图中包括哪些信息?

“正确”的定义取决于其他应用程序的开发人员,而不是您。他们如何处理您Intent上的额外内容,而不是您自己。他们对这些额外功能的处理将因应用程序,应用程序版本以及可能的设备/操作系统特性而异。您对此无能为力。您只需提供数据,然后让其他应用程序的开发人员使用它们执行所需的操作即可。

  

此代码将在Telegram中显示预览(带有突出显示的链接和预览图像),而不在WhatsApp中显示(仅显示要作为消息发送的纯文本):为什么?

因为这正是电报和WhatsApp开发人员选择的方式。

  

我也尝试过此代码,但它适用于电报,但不适用于whatsapp

该代码中存在各种错误:

  • 您在EXTRA_HTML_TEXT

  • 中提供的内容不是HTML。
  • image/jpg不是有效的MIME类型(它是image/jpeg

  • 您的Uri使用android.resource方案,而不是content方案

是否修复这些错误是否将改变WhatsApp的行为取决于WhatsApp的开发人员,并且您知道,该行为在下一小时内可能会更改七次。因此,尽管我建议修复这些错误,但不要以为任何给定应用程序的行为一定会有所不同,或者以您想要的某种方式。

答案 1 :(得分:0)

这是一个可能的答案-

Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
String text = "Sorry For Interruption,I'm Just Trying Something";
waIntent.setPackage("com.whatsapp");

if (waIntent != null) {
    waIntent.putExtra(Intent.EXTRA_TEXT, text);
    waIntent.putExtra(Intent.EXTRA_SUBJECT, "My subject");
    waIntent.putExtra(Intent.EXTRA_TITLE, "My subject");
    waIntent.putExtra(Intent.EXTRA_TEXT, "https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID);
    waIntent.putExtra(Intent.EXTRA_STREAM, attachment);
    startActivity(Intent.createChooser(waIntent,"Share with"));

希望这会有所帮助!

来源-Sending message through WhatsApp By intent

AND

https://developer.android.com/guide/components/intents-common#java

答案 2 :(得分:0)

对我来说,这是使用FileProvider的工作代码(已在具有Android 6.1的真实设备上测试):

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="app.example.com">
    <application
        ...>
        <activity
            ...></activity>
        <provider
            android:name="android.support.v4.content.FileProvider"
            android:authorities="app.example.com"
            android:exported="false"
            android:grantUriPermissions="true"
            android:readPermission="app.example.com.fileprovider.READ">
            <meta-data
                android:name="android.support.FILE_PROVIDER_PATHS"
                android:resource="@xml/provider_paths"/>
        </provider>
    </application>
</manifest>

provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
<paths>
    <external-files-path name="files" path="/" />
</paths>

Activity.java

String nomeApp = getString(R.string.app_name);
String titoloApp = getString(R.string.titolo_app);
String shareMessage = "Text message\n\n";
shareMessage = shareMessage + "https://play.google.com/store/apps/details?id=" + BuildConfig.APPLICATION_ID +"\n\n";

// use the dedicated external directory so the App doesn't need to ask for permission in manifest
File dirSaveFile = getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_PICTURES);

// create needed dirs for file path
File imagePath = new File(dirSaveFile, "external_files");
imagePath.mkdir();

// create empty file
File imageFile = new File(imagePath.getPath(), "test.jpg");

// get the Bitmap of the drawable to show
final Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.testjpg);

// write in the file the drawable image
try {
    FileOutputStream fos = new FileOutputStream(imageFile);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
    fos.flush();
    fos.close();
} catch (Exception e) {
    e.printStackTrace();
}

// create the uri
Uri uri = FileProvider.getUriForFile(this, BuildConfig.APPLICATION_ID, imageFile);

// create the Intent
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TITLE, "Title test");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject test");
shareIntent.putExtra(Intent.EXTRA_TEXT, shareMessage);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("text/plain");

startActivity(Intent.createChooser(shareIntent, getString(R.string.share)));

注意:如@CommonsWare在其回答中所述:

  

他们如何处理Intent上的额外内容取决于他们,而不是您。

因此此代码将来可能不再起作用。

相关问题