调用系统分享文字:
public static void shareText(Context context, String extraText) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "连接分享");
intent.putExtra(Intent.EXTRA_TEXT, extraText);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(
Intent.createChooser(intent, "连接分享"));
}
调用系统分享图片,方法是:
1、先读取Assets里面的图片转化成Bitmap ;
2、再以文件File形式保存在本地;
3、最后Uri连接本地该图片进行分享。
读取Assets里面的图片转化成Bitmap,代码如下:
private Bitmap getImageFromAssetsFile(String fileName)
{
Bitmap image = null;
AssetManager am = getResources().getAssets();
try
{
InputStream is = am.open(fileName);
image = BitmapFactory.decodeStream(is);
is.close();
}
catch (IOException e)
{
e.printStackTrace();
}
return image;
}
Bitmap以文件File形式保存在本地,代码如下:
public static File saveFile(Bitmap bm,String path, String fileName) throws IOException {
File dirFile = new File(path);
if(!dirFile.exists()){
dirFile.mkdir();
}
File myCaptureFile = new File(path , fileName);
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
bm.compress(Bitmap.CompressFormat.JPEG, 80, bos);
bos.flush();
bos.close();
return myCaptureFile;
}
调用系统原生分享图片代码:
public static void shareImage(Context context, Uri uri, String title) {
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
shareIntent.setType("image/jpeg");
context.startActivity(Intent.createChooser(shareIntent, title));
}
最后Uri连接本地该图片进行分享:
Bitmap bitmap = getImageFromAssetsFile("/assets/ewcode.png");
try {
File file = saveFile(bitmap, dir, "ewcode.png");
Uri uri = Uri.fromFile(file);
Shares.shareImage(EWcodeActivity.this,uri,"二维码分享");
} catch (IOException e) {
e.printStackTrace();
}
原文地址:http://blog.51cto.com/1206995290qq/2112115