我在我的应用程序中有一个共享按钮,我想同时共享图像和文本。在GMail中,它可以正常工作,但在WhatsApp中,只有图像被发送,在Facebook中应用程序崩溃。
我用来分享的代码是:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_TEXT, "Message");
Uri uri = Uri.parse("android.resource://" + getPackageName() + "/drawable/ford_focus_2014");
try {
InputStream stream = getContentResolver().openInputStream(uri);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
如果我使用" shareIntent.setType(" * / *")" Facebook和WhatsApp崩溃。
有没有办法做到这一点?也许同时发送两条消息(WhatsApp)。
提前致谢。
答案 0 :(得分:35)
目前Whatsapp同时支持图像和文本共享。 (2014年11月)。
以下是如何执行此操作的示例:
/**
* Show share dialog BOTH image and text
*/
Uri imageUri = Uri.parse(pictureFile.getAbsolutePath());
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
//Target whatsapp:
shareIntent.setPackage("com.whatsapp");
//Add text and then Image URI
shareIntent.putExtra(Intent.EXTRA_TEXT, picture_text);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/jpeg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivity(shareIntent);
} catch (android.content.ActivityNotFoundException ex) {
ToastHelper.MakeShortText("Whatsapp have not been installed.");
}
答案 1 :(得分:23)
请尝试以下代码,希望它能正常运作。
Uri imgUri = Uri.parse(pictureFile.getAbsolutePath());
Intent whatsappIntent = new Intent(Intent.ACTION_SEND);
whatsappIntent.setType("text/plain");
whatsappIntent.setPackage("com.whatsapp");
whatsappIntent.putExtra(Intent.EXTRA_TEXT, "The text you wanted to share");
whatsappIntent.putExtra(Intent.EXTRA_STREAM, imgUri);
whatsappIntent.setType("image/jpeg");
whatsappIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
activity.startActivity(whatsappIntent);
} catch (android.content.ActivityNotFoundException ex) {
ToastHelper.MakeShortText("Whatsapp have not been installed.");
}
答案 2 :(得分:8)
要在 WhatsApp 上共享文本和图片,下面会提供更多受控版本的代码,您可以添加更多与{{{}共享的方法3}},Twitter ...
public class IntentShareHelper {
public static void shareOnWhatsapp(AppCompatActivity appCompatActivity, String textBody, Uri fileUri) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.setPackage("com.whatsapp");
intent.putExtra(Intent.EXTRA_TEXT,!TextUtils.isEmpty(textBody) ? textBody : "");
if (fileUri != null) {
intent.putExtra(Intent.EXTRA_STREAM, fileUri);
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.setType("image/*");
}
try {
appCompatActivity.startActivity(intent);
} catch (android.content.ActivityNotFoundException ex) {
ex.printStackTrace();
showWarningDialog(appCompatActivity, appCompatActivity.getString(R.string.error_activity_not_found));
}
}
public static void shareOnTwitter(AppCompatActivity appCompatActivity, String textBody, Uri fileUri){...}
private static void showWarningDialog(Context context, String message) {
new AlertDialog.Builder(context)
.setMessage(message)
.setNegativeButton(context.getString(R.string.close), new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setCancelable(true)
.create().show();
}
}
要从文件中获取 Uri ,请使用以下类:
public class UtilityFile {
public static @Nullable Uri getUriFromFile(Context context, @Nullable File file) {
if (file == null)
return null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
try {
return FileProvider.getUriForFile(context, "com.my.package.fileprovider", file);
} catch (Exception e) {
e.printStackTrace();
return null;
}
} else {
return Uri.fromFile(file);
}
}
// Returns the URI path to the Bitmap displayed in specified ImageView
public static Uri getLocalBitmapUri(Context context, ImageView imageView) {
Drawable drawable = imageView.getDrawable();
Bitmap bmp = null;
if (drawable instanceof BitmapDrawable) {
bmp = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
} else {
return null;
}
// Store image to default external storage directory
Uri bmpUri = null;
try {
// Use methods on Context to access package-specific directories on external storage.
// This way, you don't need to request external read/write permission.
File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
bmpUri = getUriFromFile(context, file);
} catch (IOException e) {
e.printStackTrace();
}
return bmpUri;
}
}
要撰写 FileProvider ,请使用以下链接:Facebook
答案 3 :(得分:2)
截至目前,Whatsapp Intent
支持图片和文字:
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT,title + "\n\nLink : " + link );
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(sharePath));
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "Share image via:"));
图片将保持原样,EXTRA_TEXT
将显示为标题。
答案 4 :(得分:1)
*试一试
Uri imageUri = Uri.parse(Filepath);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setPackage("com.whatsapp");
shareIntent.putExtra(Intent.EXTRA_TEXT, "My sample image text");
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/jpeg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
startActivity(shareIntent);
} catch (android.content.ActivityNotFoundException ex) {
ToastHelper.MakeShortText("Kindly install whatsapp first");
}*
答案 5 :(得分:0)
public void shareIntentSpecificApps(String articleName, String articleContent, String imageURL) {
List<Intent> intentShareList = new ArrayList<Intent>();
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.setType("text/plain");
//shareIntent.setType("image/*");
List<ResolveInfo> resolveInfoList = getPackageManager().queryIntentActivities(shareIntent, 0);
for (ResolveInfo resInfo : resolveInfoList) {
String packageName = resInfo.activityInfo.packageName;
String name = resInfo.activityInfo.name;
Log.d("System Out", "Package Name : " + packageName);
Log.d("System Out", "Name : " + name);
if (packageName.contains("com.facebook") ||
packageName.contains("com.whatsapp")) {
Intent intent = new Intent();
intent.setComponent(new ComponentName(packageName, name));
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, articleName);
intent.putExtra(Intent.EXTRA_TEXT, articleName + "\n" + articleContent);
Drawable dr = ivArticleImage.getDrawable();
Bitmap bmp = ((GlideBitmapDrawable) dr.getCurrent()).getBitmap();
intent.putExtra(Intent.EXTRA_STREAM, getLocalBitmapUri(bmp));
intent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intentShareList.add(intent);
}
}
if (intentShareList.isEmpty()) {
Toast.makeText(ArticleDetailsActivity.this, "No apps to share !", Toast.LENGTH_SHORT).show();
} else {
Intent chooserIntent = Intent.createChooser(intentShareList.remove(0), "Share Articles");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentShareList.toArray(new Parcelable[]{}));
startActivity(chooserIntent);
}
}
您也可以在我的应用中分享图片,就像上面代码中提到的一样。
答案 6 :(得分:0)
使用此代码在whatsapp或其他带有图像和视频的程序包上共享。此处的URI是图像的路径。如果图像在内存中,则需要快速加载,如果您使用的是url,则有时图像不加载,链接直接指向。
shareBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
uri1=Uri.parse(Paths+File.separator+Img_name);
Intent intent=new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
//intent.putExtra(intent.EXTRA_SUBJECT,"Insert Something new");
String data = "Hello";
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(Intent.EXTRA_TEXT,data);
intent.putExtra(Intent.EXTRA_STREAM,uri1);
intent.setPackage("com.whatsapp");
startActivity(intent);
// end Share code
}
如果无法理解此代码,请在我的其他答案中查看完整代码。
答案 7 :(得分:0)
我对这个问题的第二个回答是:我在这里粘贴完整的代码,因为新开发人员有时需要完整的代码。
public class ImageSharer extends AppCompatActivity {
private ImageView imgView;
private Button shareBtn;
FirebaseStorage fs;
StorageReference sr,sr1;
String Img_name;
File dir1;
Uri uri1;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_sharer);
imgView = (ImageView) findViewById(R.id.imgView);
shareBtn = (Button) findViewById(R.id.shareBtn);
// Initilize firebasestorage instance
fs=FirebaseStorage.getInstance();
sr=fs.getReference();
Img_name="10.jpg";
sr1=sr.child("shiva/"+Img_name);
final String Paths= Environment.getExternalStorageDirectory()+ File.separator+"The_Bhakti"+File.separator+"Data";
dir1=new File(Paths);
if(!dir1.isDirectory())
{
dir1.mkdirs();
}
sr1.getFile(new File(dir1,Img_name)).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
sr1.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
uri1= Uri.parse(uri.toString());
}
});
}
}) ;
shareBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
uri1=Uri.parse(Paths+File.separator+Img_name);
Intent intent=new Intent(Intent.ACTION_SEND);
intent.setType("image/*");
//intent.putExtra(intent.EXTRA_SUBJECT,"Insert Something new");
String data = "Hello";
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
intent.putExtra(Intent.EXTRA_TEXT,data);
intent.putExtra(Intent.EXTRA_STREAM,uri1);
intent.setPackage("com.whatsapp");
// for particular choose we will set getPackage()
/*startActivity(intent.createChooser(intent,"Share Via"));*/// this code use for universal sharing
startActivity(intent);
// end Share code
}
});
}// onCreate closer
}
答案 8 :(得分:0)
实际上。通过将图像下载到设备外部存储,然后将图像共享到WhatsApp,可以通过WhatsApp发送图像和文本。
if (ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
Bitmap bm = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
Intent intent = new Intent(Intent.ACTION_SEND);
String share_text = "image and text";
intent.putExtra(Intent.EXTRA_TEXT, notification_share);
String path = MediaStore.Images.Media.insertImage(MainActivity.this.getContentResolver(), bm, "", null);
Uri screenshotUri = Uri.parse(path);
intent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
intent.setType("image/*");
startActivity(Intent.createChooser(intent, "Share image via..."));
} else {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
}
//The above code works perfect need to show image in an imageView
答案 9 :(得分:0)
这在2019年1月对我有用
private void shareIntent() {
Bitmap imgBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
String imgBitmapPath = MediaStore.Images.Media.insertImage(getContentResolver(),imgBitmap,"title",null);
Uri imgBitmapUri = Uri.parse(imgBitmapPath);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.putExtra(Intent.EXTRA_STREAM,imgBitmapUri);
shareIntent.setType("image/png");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.putExtra(Intent.EXTRA_TEXT, "My Custom Text ");
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "Subject text");
startActivity(Intent.createChooser(shareIntent, "Share this"));
}
这将使用户可以将图像+文本共享给WhatsApp和用户想要的所有其他应用程序,最好是让用户选择共享内容的位置,而不是仅提示WhatsApp。
还要确保,如果仅包含要共享的WhatsApp,则可能未安装在某些设备上,为此,您需要一个try catch,并在其中安装startActivity(intent);
并将意图包设置为只是带有intent.setPackage("com.whatsapp")
的WhatsApp。
答案 10 :(得分:0)
这有效:
appStatus = 'Offer Accepted'
答案 11 :(得分:-1)
从任何地方复制文本。它是Google,Facebook或whatsapo本身
尝试在whatsapp上的任何地方上传图片。联系人或群组。在您点击发送图片箭头之前...您将看到该图片的标题选项...触摸并按住,将出现粘贴选项.hit粘贴...你的文字会出现...然后你可以发送照片。它会出现你想要的文字......你去...你有文字和图像...唯一的问题是文字大小,仅限于一定数量的单词
仅适用于Android用户