我想在Android中使用ACTION_SEND共享Text + Image,我使用下面的代码,我只能共享Image但我无法与它共享Text,
private Uri imageUri;
private Intent intent;
imageUri = Uri.parse("android.resource://" + getPackageName()+ "/drawable/" + "ic_launcher");
intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, "Hello");
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
intent.setType("image/*");
startActivity(intent);
对此有何帮助?
答案 0 :(得分:43)
您可以通过这些代码分享纯文本
String shareBody = "Here is the share content body";
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, getResources().getString(R.string.share_using)));
所以您的完整代码(您的图片+文字)变为
private Uri imageUri;
private Intent intent;
imageUri = Uri.parse("android.resource://" + getPackageName()
+ "/drawable/" + "ic_launcher");
intent = new Intent(Intent.ACTION_SEND);
//text
intent.putExtra(Intent.EXTRA_TEXT, "Hello");
//image
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
//type of things
intent.setType("*/*");
//sending
startActivity(intent);
我刚刚用image/*
*/*
<强>更新强>:
Uri imageUri = Uri.parse("android.resource://" + getPackageName()
+ "/drawable/" + "ic_launcher");
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "Hello");
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/jpeg");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "send"));
答案 1 :(得分:10)
请看一下这段代码,让我一起分享文字和图片
Intent shareIntent;
Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher);
String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)+"/Share.png";
OutputStream out = null;
File file=new File(path);
try {
out = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
path=file.getPath();
Uri bmpUri = Uri.parse("file://"+path);
shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.putExtra(Intent.EXTRA_TEXT,"Hey please check this application " + "https://play.google.com/store/apps/details?id=" +getPackageName());
shareIntent.setType("image/png");
startActivity(Intent.createChooser(shareIntent,"Share with"));
不要忘记提供WRITE_EXTERNAL_STORAGE权限
同样在Facebook中它只能共享图像,因为Facebook不允许通过意图分享文本
答案 2 :(得分:6)
可能是因为共享应用程序(FB,Twitter等)可能没有阅读图像的权限。
谷歌的文件说:
接收应用程序需要访问Uri指向的数据的权限。建议的方法是:
http://developer.android.com/training/sharing/send.html
我不确定共享应用是否有权阅读我们应用包中的图片。但是我的文件保存在
中 Activity.getFilesDir()
无法 被阅读。如上述链接所示,我们可能会考虑将图像存储在MediaStore中,共享应用程序具有读取权限。
<强> UPDATE1:强> 以下是在Android 4.4中使用文本共享图像的工作代码。
Bitmap bm = BitmapFactory.decodeFile(file.getPath());
intent.putExtra(Intent.EXTRA_TEXT, Constant.SHARE_MESSAGE
+ Constant.SHARE_URL);
String url= MediaStore.Images.Media.insertImage(this.getContentResolver(), bm, "title", "description");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(url));
intent.setType("image/*");
startActivity(Intent.createChooser(intent, "Share Image"));
副作用是我们在MediaStore中添加图像。
答案 3 :(得分:1)
尝试使用此代码。我从drawable上传ic_launcher。您可以使用gallary或bitmap中的文件更改此内容。
void share() {
Bitmap icon = BitmapFactory.decodeResource(getResources(),
R.drawable.ic_launcher);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("image/jpeg");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
icon.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
File f = new File(Environment.getExternalStorageDirectory()
+ File.separator + "temporary_file.jpg");
try {
f.createNewFile();
FileOutputStream fo = new FileOutputStream(f);
fo.write(bytes.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
share.putExtra(Intent.EXTRA_TEXT, "hello #test");
share.putExtra(Intent.EXTRA_STREAM,
Uri.parse("file:///sdcard/temporary_file.jpg"));
startActivity(Intent.createChooser(share, "Share Image"));
}
&#13;
答案 4 :(得分:1)
我一直在寻找这个问题的解决方案一段时间,并发现这个问题正在运行,希望它有所帮助。
private BitmapDrawable bitmapDrawable;
private Bitmap bitmap1;
//write this code in your share button or function
bitmapDrawable = (BitmapDrawable) mImageView.getDrawable();// get the from imageview or use your drawable from drawable folder
bitmap1 = bitmapDrawable.getBitmap();
String imgBitmapPath= MediaStore.Images.Media.insertImage(getContentResolver(),bitmap1,"title",null);
Uri imgBitmapUri=Uri.parse(imgBitmapPath);
String shareText="Share image and text";
Intent shareIntent=new Intent(Intent.ACTION_SEND);
shareIntent.setType("*/*");
shareIntent.putExtra(Intent.EXTRA_STREAM,imgBitmapUri);
shareIntent.putExtra(Intent.EXTRA_TEXT, shareText);
startActivity(Intent.createChooser(shareIntent,"Share Wallpaper using"));
答案 5 :(得分:1)
String text = "Look at my awesome picture";
Uri pictureUri = Uri.parse("file://my_picture");
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, text);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(shareIntent, "Share images..."));
答案 6 :(得分:1)
检查以下对我有用的代码
Picasso.with(getApplicationContext()).load("image url path").into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Intent i = new Intent(Intent.ACTION_SEND);
i.putExtra(Intent.EXTRA_TEXT, "Let me recommend you this application" +
"\n"+ "your share url or text ");
i.setType("image/*");
i.putExtra(Intent.EXTRA_STREAM, getLocalBitmapUri(bitmap));
context.startActivity(Intent.createChooser(i, "Share using"));
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
private Uri getLocalBitmapUri(Bitmap bmp) {
Uri bmpUri = null;
try {
File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + System.currentTimeMillis() + ".png");
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 50, out);
out.close();
bmpUri = Uri.fromFile(file);
} catch (IOException e) {
e.printStackTrace();
}
return bmpUri;
}
答案 7 :(得分:1)
要共享可绘制图像,必须首先将图像保存在设备的缓存或外部存储中。
我们检查缓存中是否已经存在“ sharable_image.jpg”,如果存在,则从现有文件中检索路径。
否则,可绘制图像将保存在缓存中。
然后使用意图共享保存的图像。
private void share() {
int sharableImage = R.drawable.person2;
Bitmap bitmap= BitmapFactory.decodeResource(getResources(), sharableImage);
String path = getExternalCacheDir()+"/sharable_image.jpg";
java.io.OutputStream out;
java.io.File file = new java.io.File(path);
if(!file.exists()) {
try {
out = new java.io.FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
out.flush();
out.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
path = file.getPath();
Uri bmpUri = Uri.parse("file://" + path);
Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.putExtra(Intent.EXTRA_TEXT, "This is a sample body with more detailed description");
shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent,"Share with"));
}
答案 8 :(得分:1)
private void shareImage(){
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());
Bitmap bitmap= BitmapFactory.decodeResource(getResources(),R.drawable.Starlay_straightface_image);
File f = new File(getExternalCacheDir()+"/"+getResources().getString(R.string.app_name)+".png");
Intent shareIntent;
try {
FileOutputStream outputStream = new FileOutputStream(f);
bitmap.compress(Bitmap.CompressFormat.PNG,100,outputStream);
outputStream.flush();
outputStream.close();
shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/*");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(f));
shareIntent.putExtra(Intent.EXTRA_TEXT,"Hey please check this application " + "https://play.google.com/store/apps/details?id=" +getPackageName());
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}catch (Exception e){
throw new RuntimeException(e);
}
startActivity(Intent.createChooser(shareIntent,"Share Picture"));
}
答案 9 :(得分:0)
偶然(文本消息部分,我放弃了),我注意到当我选择Messages App来处理请求时,消息应用程序将打开来自Intent.EXTRA_SUBJECT的文本以及准备发送的图像,我希望它有所帮助。
String[] recipient = {"your_email_here"};
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL, recipient);
intent.putExtra(Intent.EXTRA_SUBJECT, "Hello, this is the subject line");
intent.putExtra(Intent.EXTRA_TEXT, messageEditText.getText().toString());
intent.putExtra(Intent.EXTRA_STREAM, imageUri);
intent.setType("image/*");
答案 10 :(得分:0)
String bitmapPath = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "title", null);
Uri bitmapUri = Uri.parse(bitmapPath);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/png");
intent.putExtra(Intent.EXTRA_STREAM, bitmapUri);
intent.putExtra(Intent.EXTRA_TEXT, "This is a playstore link to download.. " + "https://play.google.com/store/apps/details?id=" + getPackageName());
startActivity(Intent.createChooser(intent, "Share"));
答案 11 :(得分:0)
我在 Android X 上尝试了 this 解决方案,并且与 whatsapp、telegram 和 gmail 完美配合。 这是我的代码:
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_TEXT, "<<sharingText>>");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("<<imageResource>>"));
shareIntent.setType("image/*");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (getView() != null && getView().getContext() != null &&
shareIntent.resolveActivity(getView().getContext().getPackageManager()) != null) {
getView().getContext().startActivity(Intent.createChooser(shareIntent, null));
}