最近项目要求有微博分享功能,于是在网上搜索了一些资料,大概分为两种情况:
1、利用SNS开放平台;
2、利用手机Intent.ACTION_SEND,让手机里面的微博apk来分享(利:开发快,也不用授权;弊:要是用户手机没有安装SNS APK 就不能分享了,一般要分享的人手机都安装了SNS 应用的 )
先说SNS开放平台, 这个就要求开发者去开放平台注册应用,下载SDK包,再进行授权分享。过程也比较麻烦,这里就不详述。
而现在主要来说第二种:
分享的图片在assets下面,用于分享出去是给第三方应用来调用图片 所以先把图片复制到SD卡上面 这样微博apk就可以调用图片了
直接上代码:
Intent shareIntent = new Intent(Intent.ACTION_SEND); shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
String ALBUM_PATH = Environment.getExternalStorageDirectory() + "/img/";
if (Environment.getExternalStorageState().equals(//看SD卡可以授权写不
Environment.MEDIA_MOUNTED)) {
try {
InputStream is = SafeSettingsActivity.this.getAssets()
.open("weibo.jpg");
Bitmap bm = BitmapFactory.decodeStream(is);
File dirFile = new File(ALBUM_PATH);
if(!dirFile.exists()){
dirFile.mkdir();
}
File myCaptureFile = new File(ALBUM_PATH + "weibo.jpg");
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
bm.compress(Bitmap.CompressFormat.JPEG, 100, bos);
bos.flush();
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
shareIntent.putExtra(Intent.EXTRA_STREAM,
Uri.parse("file:///sdcard/img/weibo.jpg"));
shareIntent.setType("image/*");// 分享的数据类型
}else {
shareIntent.setType("text/plain");
}
shareIntent.putExtra(Intent.EXTRA_SUBJECT, "分享"); // 主题
shareIntent
.putExtra(
Intent.EXTRA_TEXT,"分享的内容"); // 内容
startActivity(Intent.createChooser(shareIntent, "分享APK"));
利用该代码就可以分享文字和图片了 呵呵