博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android简单分享----文字加图片
阅读量:6971 次
发布时间:2019-06-27

本文共 3067 字,大约阅读时间需要 10 分钟。

hot3.png

这是android的简单分享,如果想分享到微博这些的话,需要你的手机里安装相关的app,下面的这些代码是整理网上各种网友的代码而得来的,感谢分享!
使用这个类的方法:
1、肯定是导入下面这个类啦
2、在你的Activity加入下面几行代码
WindowManager windowManager = getWindowManager();View decorview = YourActivity.this .getWindow().getDecorView();                         ShareToSNS my_share = new ShareToSNS();                                        startActivity(Intent. createChooser(                                my_share.getIntentSharePhotoAndText( my_share.GetandSaveCurrentImage(windowManager,decorview),"发送到编辑框的文本内容"), "分享" ));

类的代码

/***这是分享文字加图片的类*/import java.io.File;import java.io.FileOutputStream;import android.app.Activity;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.Bitmap.Config;import android.net.Uri;import android.os.Environment;import android.view.Display;import android.view.View;import android.view.WindowManager;import android.widget.Toast;public class ShareToSNS  {     private String SavePath;     private String image_name = "/Scinan_Screen.png";         public String GetandSaveCurrentImage(WindowManager windowManager,View decorview)     {         //1.构建Bitmap         Display display = windowManager.getDefaultDisplay();         int w = display.getWidth();         int h = display.getHeight();                  Bitmap Bmp = Bitmap.createBitmap( w, h, Config.ARGB_8888 );                      //2.获取屏幕                 decorview.setDrawingCacheEnabled(true);          Bmp = decorview.getDrawingCache();                 SavePath = getSDCardPath()+"/AndyDemo/ScreenImage";             //3.保存Bitmap          try {             File path = new File(SavePath);             //文件             String filepath = SavePath + image_name;             File file = new File(filepath);             if(!path.exists()){                 path.mkdirs();             }             if (!file.exists()) {                 file.createNewFile();             }                          FileOutputStream fos = null;             fos = new FileOutputStream(file);             if (null != fos) {                 Bmp.compress(Bitmap.CompressFormat.PNG, 90, fos);                 fos.flush();                 fos.close();                       }         } catch (Exception e) {                     }          return SavePath;    }        private String getSDCardPath(){         File sdcardDir = null;         //判断SDCard是否存在         boolean sdcardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);         if(sdcardExist){              sdcardDir = Environment.getExternalStorageDirectory();         }         return sdcardDir.toString();    }            // 分享照片    public Intent getIntentSharePhotoAndText(String photoUri,String share_text) {        Intent shareIntent = new Intent(Intent.ACTION_SEND);        File file = new File(photoUri + image_name);        shareIntent.putExtra(Intent.EXTRA_TEXT, share_text);        shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));        shareIntent.setType("image/png");        return shareIntent;    }}
  

转载于:https://my.oschina.net/kevinvane/blog/79384

你可能感兴趣的文章
WPF DrawingContext Pen
查看>>
Wordpress+LNMP 环境搭建
查看>>
小步快跑是这样玩的(下)
查看>>
rsync工作方式介绍03
查看>>
Windows 7蓝屏自动重启,无法修复的处理办法
查看>>
SSH-Struts2、Spring和Hibernate应用实例
查看>>
Objective-C 类别(Category)浅谈
查看>>
Panasonic多方电话会议
查看>>
html
查看>>
div css 设置文字垂直居中
查看>>
我的友情链接
查看>>
PLSQL基础
查看>>
不同CDB之间迁移PDB
查看>>
Amoeba for MySQL 读写分离配置
查看>>
我的友情链接
查看>>
netbase3监控mysql
查看>>
java开发常见的一些问题总结
查看>>
百度地图利用DrivingRoute做轨迹回放
查看>>
Nginx if 条件判断
查看>>
联想y460 +centos6.4 64位 开启无线热点Ap
查看>>