如何将我的应用程序中的图片共享给Whatsapp?

时间:2015-02-11 21:27:08

标签: android share whatsapp

我在这里搜索了一些解决方案,但仅针对 iOS 。如何将我的 Android 应用程序中的图片分享给Whatsapp?

2 个答案:

答案 0 :(得分:3)

答案 1 :(得分:2)

试试这段代码。

public void shareImageWhatsApp() {

    Bitmap adv = BitmapFactory.decodeResource(getResources(), R.drawable.adv);
    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/jpeg");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    adv.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    File f = new File(Environment.getExternalStorageDirectory()
            + File.separator + "temporary_file.jpg");
    try {
        f.createNewFile();
        new FileOutputStream(f).write(bytes.toByteArray());
    } catch (IOException e) {
        e.printStackTrace();
    }
    share.putExtra(Intent.EXTRA_STREAM,
            Uri.parse( Environment.getExternalStorageDirectory()+ File.separator+"temporary_file.jpg"));
    if(isPackageInstalled("com.whatsapp",this)){
          share.setPackage("com.whatsapp"); 
          startActivity(Intent.createChooser(share, "Share Image"));

    }else{

        Toast.makeText(getApplicationContext(), "Please Install Whatsapp", Toast.LENGTH_LONG).show();
    }

}

private boolean isPackageInstalled(String packagename, Context context) {
    PackageManager pm = context.getPackageManager();
    try {
        pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
        return true;
    } catch (NameNotFoundException e) {
        return false;
    }
}