在Whatsapp中发送图片

时间:2013-03-25 13:53:42

标签: android image android-intent android-gallery whatsapp

我想在Whatsapp中发送图片。当我选择Whatsapp中的图像选择器时,我的应用程序启动。如何将Intent的结果发送回whatsapp?

我使用以下代码:

         // on button press
            String path = SaveCache(R.drawable.pic_1);

            Intent share = new Intent(android.content.Intent.ACTION_SEND);
            share.setType("image/*");
            share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + path));  


        }

}


private String SaveCache(int resID) {
    String path = "";  
    try {
        InputStream is = getResources().openRawResource(resID);
        File cacheDir = context.getExternalCacheDir();
        File downloadingMediaFile = new File(cacheDir, "abc.jpg");
        byte[] buf = new byte[256];
        FileOutputStream out = new FileOutputStream(downloadingMediaFile);   
        while (true) {
            int rd = is.read(buf, 0, 256);
            if (rd == -1 || rd == 0)
                break;
            out.write(buf, 0, rd);              
        }
        is.close();
        out.close();
        return downloadingMediaFile.getPath();
    } catch (Exception ex) {

        ex.printStackTrace();
    }
    return path;
}

1 个答案:

答案 0 :(得分:0)

我能够使用此代码发送图像

Uri uri = Uri.parse("android.resource://com.example.test/drawable/image_1");
                sharingIntent.setType("image/jpg"); 
                sharingIntent.putExtra(Intent.EXTRA_STREAM, uri); 
                startActivity(Intent.createChooser(sharingIntent, "Share image using"));