【问题标题】:How to create a "general" share intent in android ?如何在 android 中创建“通用”共享意图?
【发布时间】:2014-03-06 05:04:11
【问题描述】:

我正在研究android中的共享功能,从在线教程中我找到了示例代码并创建了这样的功能。但它有一些缺点(如果我安装了很多应用程序有太多的选项),所以,总之,我想做以下事情:

  1. 严格选择facebook twitter gmail
  2. 如何分享图片(如果我提供链接?(twitter 似乎只接受纯文本))
  3. 以下代码在 facebook 应用程序中没有显示任何内容?(无法在 facebook 上共享数据)

这是我的代码:

Intent sendIntent = new Intent(Intent.ACTION_SEND);
                sendIntent.setType("text/plain");
                try {
                    String newsUrl = Html.fromHtml( URLDecoder.decode(content[0], "UTF-8")).toString().replace("appfunc://share=", "");
                    String title = Html.fromHtml( URLDecoder.decode(content[1], "UTF-8")).toString();
                    String newsContent = Html.fromHtml( URLDecoder.decode(content[2], "UTF-8")).toString();
                    if (!newsContent.equals(""))
                        newsContent += "...\n\n";
                    sendIntent.putExtra(Intent.EXTRA_TEXT, title + "\n\n" + newsContent + ctx.getResources().getString(R.string.link) + ": " + newsUrl);
                } catch (UnsupportedEncodingException e) {
                    // TODO Auto-generated catch block
                    sendIntent.putExtra(Intent.EXTRA_TEXT, ctx.getResources().getString(R.string.share_placeholder));
                    e.printStackTrace();
                };
                ctx.startActivity(Intent.createChooser(sendIntent, ctx.getResources().getString(R.string.share_to)));

【问题讨论】:

标签: android facebook android-intent share


【解决方案1】:

您可以像这样使用 shareIntent 共享图像

private Intent createShareIntent()
 {
    shareIntent = new Intent(Intent.ACTION_SEND);
    shareIntent.setType("image/*");
    shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+CreateTempFile(bitmapImage)));
    return shareIntent;
}
private File CreateTempFile(Bitmap myBitmap)
{

     try 
     {
    File SharingFile = File.createTempFile("OriginalImage", ".jpeg",temporaryFile);
        FileOutputStream out = new FileOutputStream(SharingFile);
        myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
        out.flush();
        out.close();

    } 
     catch (IOException e) 
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


return SharingFile;

}

这样做会打开所有接受您的图像的应用程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多