用 Intent 分享图片和文字到 Whatsapp
Share image and text with Intent to Whats App
我正在尝试将 imageview
中带有文字说明的图片分享到 whatsApp,但我在网上找到的解决方案似乎对我不起作用。
View content = findViewById(R.id.posted_house);
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
File root = Environment.getExternalStorageDirectory();
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");
try
{
root.createNewFile();
FileOutputStream ostream = new FileOutputStream(cachePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
Intent txtIntent = new Intent(android.content.Intent.ACTION_SEND);
txtIntent .setType("image/*");
txtIntent .putExtra("message");
txtIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(cachePath));
startActivity(Intent.createChooser(txtIntent ,"Share"));
}
这是在 whatsapp 中分享图片和文字的代码..
View screenView = rootView.getRootView();
screenView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache());
screenView.setDrawingCacheEnabled(false);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(this.getContentResolver(), bitmap, "Title", null);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/*");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "");
intent.putExtra(android.content.Intent.EXTRA_TEXT, "Your message");
intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(path));
try {
startActivity(Intent.createChooser(intent, "Share Screenshot"));
} catch (ActivityNotFoundException e) {
Toast.makeText(this, "No App Available", Toast.LENGTH_SHORT).show();
}
我正在尝试将 imageview
中带有文字说明的图片分享到 whatsApp,但我在网上找到的解决方案似乎对我不起作用。
View content = findViewById(R.id.posted_house);
content.setDrawingCacheEnabled(true);
Bitmap bitmap = content.getDrawingCache();
File root = Environment.getExternalStorageDirectory();
File cachePath = new File(root.getAbsolutePath() + "/DCIM/Camera/image.jpg");
try
{
root.createNewFile();
FileOutputStream ostream = new FileOutputStream(cachePath);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, ostream);
ostream.close();
}
catch (Exception e)
{
e.printStackTrace();
}
Intent txtIntent = new Intent(android.content.Intent.ACTION_SEND);
txtIntent .setType("image/*");
txtIntent .putExtra("message");
txtIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(cachePath));
startActivity(Intent.createChooser(txtIntent ,"Share"));
}
这是在 whatsapp 中分享图片和文字的代码..
View screenView = rootView.getRootView(); screenView.setDrawingCacheEnabled(true); Bitmap bitmap = Bitmap.createBitmap(screenView.getDrawingCache()); screenView.setDrawingCacheEnabled(false); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, bytes); String path = MediaStore.Images.Media.insertImage(this.getContentResolver(), bitmap, "Title", null); Intent intent = new Intent(); intent.setAction(Intent.ACTION_SEND); intent.setType("image/*"); intent.putExtra(android.content.Intent.EXTRA_SUBJECT, ""); intent.putExtra(android.content.Intent.EXTRA_TEXT, "Your message"); intent.putExtra(Intent.EXTRA_STREAM, Uri.parse(path)); try { startActivity(Intent.createChooser(intent, "Share Screenshot")); } catch (ActivityNotFoundException e) { Toast.makeText(this, "No App Available", Toast.LENGTH_SHORT).show(); }