是否可以通过相同的“Intent.ACTION_SEND”将图像与文本同时共享到 Viber 或 Facebook Messenger?我有代码,但它只发送没有文本的图像。是否可以使用相同的 ACTION_SEND 发送图像 + 文本?它适用于电子邮件共享或 SMS 共享,图像和文本共享,但在 Viber 或 Facebook 中,仅图像共享。有人可以给我工作解决方案如何解决它吗?
非常感谢您。
活动:
package com.my.game;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;
import java.io.File;
import java.io.FileOutputStream;
public class shareActivity extends AppCompatActivity {
Button share;
ImageView imageView;
static Activity activity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share);
share = findViewById(R.id.shareTestButton);
imageView = findViewById(R.id.shareimage);
activity = this;
// initialising text field where we will enter data
share.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Now share image function will be called
// here we will be passing the text to share
// Getting drawable value from image
BitmapDrawable bitmapDrawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = bitmapDrawable.getBitmap();
shareImageandText(bitmap);
}
});
}
private void shareImageandText(Bitmap bitmap) {
Uri uri = getmageToShare(bitmap);
Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_STREAM, uri);
i.putExtra(Intent.EXTRA_TEXT, "\nI Am Rich Pro - Luxury Boss \n\n" +
"I am a member of the billionaire club. My Rich Level: 3/5\n\n" +
"Download now: https://play.google.com/store/apps/details?id=com.my.game \n\n"+
"Can you beat my rich level? Proove it!");
i.setType("image/*");
shareActivity.this.startActivity(Intent.createChooser(i, "choose one"));
}
// Retrieving the url to share
private Uri getmageToShare(Bitmap bitmap) {
File imagefolder = new File(getCacheDir(), "images");
Uri uri = null;
try {
imagefolder.mkdirs();
File file = new File(imagefolder, "shared_image.png");
FileOutputStream outputStream = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, outputStream);
outputStream.flush();
outputStream.close();
uri = FileProvider.getUriForFile(this, "com.thedgames.iamrich.fileprovider", file);
} catch (Exception e) {
Toast.makeText(this, "" + e.getMessage(), Toast.LENGTH_LONG).show();
}
return uri;
}
}
布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".shareActivity">
<!-- Here we will attach our image to share -->
<ImageView
android:id="@+id/shareimage"
android:layout_width="200dp"
android:layout_height="200dp"
android:src="@drawable/silver_card" />
<!-- We will click on it then shareonlytext
function will be called-->
<Button
android:id="@+id/shareTestButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:padding="10dp"
android:text="Click here to Share "
android:textSize="10dp" />
</LinearLayout>
安卓清单:
<activity
android:name=".shareActivity"
android:exported="true"
android:theme="@style/AppFullScreenTheme"/>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.my.game.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/paths" />
</provider>