2

我想在 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;
}
4

1 回答 1

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"));
于 2014-09-12T19:29:48.623 回答