调用系统分享文字:
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里面
2、先读取Assets里面的图片转化成Bitmap;
3、再以文件File形式保存在本地;
4、最后Uri连接本地该图片进行分享。
调用系统原生分享图片代码:
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连接本地该图片进行分享:
方法一:
// 读取Assets里面的图片转化成Bitmap
private static Bitmap getImageFromAssetsFile(Context context,String fileName){
Bitmap image = null;
AssetManager am = context.getResources().getAssets();
try {
InputStream is=am.open(fileName);
image= BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace(); }
return image;
}
// Bitmap以文件File形式保存在本地
private static Uri saveBitmap(Bitmap bm, String picName) {
try {
String dir= Environment.getExternalStorageDirectory().getAbsolutePath()+"/zqhd/"+picName+".jpg";
File f = new File(dir);
if (!f.exists()) {
f.getParentFile().mkdirs();
f.createNewFile();
}
FileOutputStream out = new FileOutputStream(f);
bm.compress(Bitmap.CompressFormat.JPEG, 80, out);
out.flush();
out.close();
Uri uri = Uri.fromFile(f);
return uri;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace(); }
return null;
}
public static void localshare(Context context,String pic) {
/* 分享图片 /
Bitmap bgimg0 = getImageFromAssetsFile(context,pic+".png");
Intent share_intent = new Intent();
share_intent.setAction(Intent.ACTION_SEND);//设置分享行为
share_intent.setType("image/"); //设置分享内容的类型
share_intent.putExtra(Intent.EXTRA_STREAM, saveBitmap(bgimg0,pic));
//创建分享的Dialog
share_intent = Intent.createChooser(share_intent, "分享图片");
context.startActivity(share_intent);
}
方法二:
public static String dir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/zuhd/";
// 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 sharePic(){
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.mipmap.ewcode);
File file=null;
try {
file = saveFile(bitmap, dir, "ewcode.jpg");
} catch (IOException e) {
e.printStackTrace();
}
Uri uri = Uri.fromFile(file);
Shares.shareImage(EWcodeActivity.this,uri,"二维码分享");
}