android调用系统分享图片及文字

  

调用系统分享文字:
公共静态孔隙shareText(上下文语境,字符串extraText) {
的目的意图=new意图(Intent.ACTION_SEND);
intent.setType (“text/plain");
intent.putExtra(意图。EXTRA_SUBJECT,“连接分享“);
intent.putExtra(意图。EXTRA_TEXT extraText);
intent.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity (
意图。createChooser(意图,“连接分享“));
}

  

调用系统分享图片,方法是:
1,把图片放到文件资产里面
2,先读取资产里面的图片转化成位图;
3,再以文件文件形式保存在本地;
4,最后Uri连接本地该图片进行分享。

  

调用系统原生分享图片代码:

  

公共静态孔隙shareImage(上下文语境、Uri Uri字符串标题){
意图shareIntent=new意图();
shareIntent.setAction (Intent.ACTION_SEND);
shareIntent.putExtra(意图。EXTRA_STREAM uri),
shareIntent.setType(“图像/jpeg");
context.startActivity(意图。createChooser (shareIntent、标题));
}

  

最后Uri连接本地该图片进行分享:

  

方法一:
//读取资产里面的图片转化成位图
私有静态位图getImageFromAssetsFile(上下文语境,字符串文件名){
位图图像=零;
AssetManager我=context.getResources () .getAssets ();
尝试{
InputStream是=am.open(文件名);
=BitmapFactory.decodeStream形象(是),
is.close ();
}捕捉(IOException e) {
e.printStackTrace ();}
返回图像;
}

  

//位图以文件文件形式保存在本地
私有静态Uri saveBitmap(位图bm,字符串picName) {
尝试{
字符串dir=Environment.getExternalStorageDirectory () .getAbsolutePath () +“zqhd/? picName +“.jpg";
文件f=新文件(dir);
如果(! f.exists ()) {
f.getParentFile () .mkdirs ();
f.createNewFile ();
}
FileOutputStream=新FileOutputStream (f);
bm.compress (Bitmap.CompressFormat.JPEG, 80年),
out.flush ();
out.close ();
Uri Uri=Uri.fromFile (f);
返回Uri;
}捕捉(FileNotFoundException e) {
e.printStackTrace ();
}捕捉(IOException e) {
e.printStackTrace ();}
返回null;
}

  

公共静态孔隙localshare(上下文背景下,字符串图片){
/* 分享图片/
位图bgimg0=getImageFromAssetsFile(上下文,图片+“.png");
意图share_intent=new意图();
share_intent.setAction (Intent.ACTION_SEND);//设置分享行为
share_intent.setType(“图像/
“);//设置分享内容的类型
share_intent.putExtra(意图。EXTRA_STREAM saveBitmap (bgimg0图片));
//创建分享的对话框
share_intent=意图。createChooser (share_intent“分享图片“);
context.startActivity (share_intent);
}

  

方法二:

  

公共静态字符串dir=Environment.getExternalStorageDirectory () .getAbsolutePath () +“/zuhd/?

  

//位图以文件文件形式保存在本地
公共静态文件saveFile(位图bm,路径的字符串,字符串文件名)抛出IOException {
文件dirFile=新文件(路径),
如果(! dirFile.exists ()) {
dirFile.mkdir ();
}
文件myCaptureFile=新文件(路径,文件名);
BufferedOutputStream bos=new BufferedOutputStream(新FileOutputStream (myCaptureFile));
bm.compress(80年Bitmap.CompressFormat.JPEG bos);
bos.flush ();
bos.close ();
返回myCaptureFile;
}

  
 <代码>公共静态孔隙sharePic () {
  位图的位图=BitmapFactory.decodeResource (getresource (), R.mipmap.ewcode);
  文件文件=零;
  尝试{
  文件=saveFile(位图,dir,“ewcode.jpg”);
  
  }捕捉(IOException e) {
  e.printStackTrace ();
  }
  Uri Uri=Uri.fromFile(文件);
  Shares.shareImage (EWcodeActivity.this uri,“二维码分享”);
  
  } 

android调用系统分享图片及文字