如何与WhatsApp Android共享图像?

时间:2015-07-10 23:00:07

标签: android image share whatsapp

我正在尝试将此代码发送到WhatsApp,但是当它打开WhatsApp屏幕时,当我选择联系人时,我看不到任何图像。有人能帮我吗?

    Bitmap adv = BitmapFactory.decodeResource(getResources(), R.drawable.image3);
    Intent share = new Intent(Intent.ACTION_SEND);
    share.setType("image/jpg");
    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();
    }
    Uri imageUri = Uri.parse(f.getAbsolutePath());
    share.putExtra(Intent.EXTRA_STREAM, Uri.parse(Environment.getExternalStorageDirectory() + File.separator + "temporary_file.jpg"));
    //share.putExtra(Intent.EXTRA_STREAM,Uri.parse("file:///android_asset/image3.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 (PackageManager.NameNotFoundException e) {
        return false;
    }
}

0 个答案:

没有答案