在whatsapp中一起分享图片和文字
Share Image and text together in whatsapp
我正在尝试通过我的应用程序在 whatsapp 中分享文本+图像。
但我无法分享图片,只有文字 shared.the 图片区域显示为空白。
这是我的代码
Uri uri = Uri.fromFile(new File(fname));
Intent share = new Intent();
share.setAction(Intent.ACTION_SEND);
share.setPackage("com.whatsapp");
share.putExtra(Intent.EXTRA_TEXT,ci.description);
share.putExtra(Intent.EXTRA_STREAM,uri);
share.setType("image/*");
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
context.startActivity(share);
}catch (Exception what){
Toast.makeText(context,"Whatsapp have not been installed",Toast.LENGTH_LONG).show();
}
这是我的截图
谁能帮帮我?
也许你的 URI 有问题..试试这个答案
意图代码:
Intent i = new Intent(Intent.ACTION_SEND);
i.setPackage("com.whatsapp");
i.setType("image/*");
i.putExtra(Intent.EXTRA_STREAM, getLocalBitmapUri(context, bitmap, id));
i.putExtra(Intent.EXTRA_TEXT, text);
context.startActivity(Intent.createChooser(i, "Share News"));
位图到 URI 代码:(如果有文件 URI 跳过此)
public Uri getLocalBitmapUri(Context context, Bitmap bmp, String id) {
Uri bmpUri = null;
try {
if (id == null) {
id = String.valueOf(System.currentTimeMillis());
}
File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + id + ".png");
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
bmpUri = Uri.fromFile(file);
} catch (Exception e) {
Log.e(TAG, "getLocalBitmapUri: ", e);
}
return bmpUri;
}
我正在尝试通过我的应用程序在 whatsapp 中分享文本+图像。
但我无法分享图片,只有文字 shared.the 图片区域显示为空白。
这是我的代码
Uri uri = Uri.fromFile(new File(fname));
Intent share = new Intent();
share.setAction(Intent.ACTION_SEND);
share.setPackage("com.whatsapp");
share.putExtra(Intent.EXTRA_TEXT,ci.description);
share.putExtra(Intent.EXTRA_STREAM,uri);
share.setType("image/*");
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
try {
context.startActivity(share);
}catch (Exception what){
Toast.makeText(context,"Whatsapp have not been installed",Toast.LENGTH_LONG).show();
}
这是我的截图
谁能帮帮我?
也许你的 URI 有问题..试试这个答案
意图代码:
Intent i = new Intent(Intent.ACTION_SEND);
i.setPackage("com.whatsapp");
i.setType("image/*");
i.putExtra(Intent.EXTRA_STREAM, getLocalBitmapUri(context, bitmap, id));
i.putExtra(Intent.EXTRA_TEXT, text);
context.startActivity(Intent.createChooser(i, "Share News"));
位图到 URI 代码:(如果有文件 URI 跳过此)
public Uri getLocalBitmapUri(Context context, Bitmap bmp, String id) {
Uri bmpUri = null;
try {
if (id == null) {
id = String.valueOf(System.currentTimeMillis());
}
File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES), "share_image_" + id + ".png");
FileOutputStream out = new FileOutputStream(file);
bmp.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
bmpUri = Uri.fromFile(file);
} catch (Exception e) {
Log.e(TAG, "getLocalBitmapUri: ", e);
}
return bmpUri;
}