我正在研究android中的共享功能,从在线教程中我找到了示例代码并创建了这样的函数。但它有一些缺点(如果我安装了很多应用程序,有太多选项),所以,简而言之,我想做以下事情:
这是我的代码:
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)));
答案 0 :(得分:2)
您可以像使用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;
}
执行此类操作会打开所有可接受您图片的应用。