分享功能的实现方式

来源:互联网 发布:小学生上网数据 编辑:程序博客网 时间:2024/05/01 16:33

1.通过intent调用系统中自带的分享功能。

  1.     //分享文字  
  2.     publicvoid shareText(View view) {  
  3.         Intent shareIntent = newIntent();  
  4.         shareIntent.setAction(Intent.ACTION_SEND);  
  5.         shareIntent.putExtra(Intent.EXTRA_TEXT, "描述信息" + "这里你可以追加一个url连接"););  
  6.         shareIntent.setType("text/plain");  
  7.    
  8.         //设置分享列表的标题,并且每次都显示分享列表  
  9.         startActivity(Intent.createChooser(shareIntent,"分享到"));  
  10.     }  
  11.    
  12.     //分享单张图片  
  13.     publicvoid shareSingleImage(View view) {  
  14.         String imagePath = Environment.getExternalStorageDirectory() + File.separator + "test.jpg";  
  15.         //由文件得到uri  
  16.         Uri imageUri = Uri.fromFile(newFile(imagePath));  
  17.         Log.d("share","uri:"+ imageUri);  //输出:file:///storage/emulated/0/test.jpg  
  18.    
  19.         Intent shareIntent = newIntent();  
  20.         shareIntent.setAction(Intent.ACTION_SEND);  
  21.         shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);  
  22.         shareIntent.setType("image/*");  
  23.         startActivity(Intent.createChooser(shareIntent,"分享到"));  
  24.     }  
  25.    
  26.     //分享多张图片  
  27.     publicvoid shareMultipleImage(View view) {  
  28.         ArrayList<uri> uriList = newArrayList<>();  
  29.    
  30.         String path = Environment.getExternalStorageDirectory() + File.separator;  
  31.         uriList.add(Uri.fromFile(newFile(path+"australia_1.jpg")));  
  32.         uriList.add(Uri.fromFile(newFile(path+"australia_2.jpg")));  
  33.         uriList.add(Uri.fromFile(newFile(path+"australia_3.jpg")));  
  34.    
  35.         Intent shareIntent = newIntent();  
  36.         shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);  
  37.         shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList);  
  38.         shareIntent.setType("image/*");  
  39.         startActivity(Intent.createChooser(shareIntent,"分享到"));  
  40.     } 
通常我们分享的图片不可能是本地存好的图片,我们通常分享的是自己生成的图片获者网络图片
这里简单给大家介绍一个仿好奇心日报那样的把本地布局截图分享出去

1,首先获取截图
/*
    * 将布局转化为bitmap
这里传入的是你要截的布局的根View
    * */
    public Bitmap getBitmapByView(View headerView) {
        int h = headerView.getHeight();
        Bitmap bitmap = Bitmap.createBitmap(headerView.getWidth(), h, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        headerView.draw(canvas);
        return bitmap;
    }

2,把截图获取的bitmap做简单的压缩
 /*
       * 压缩图片
       * */
    private Bitmap compressImage(Bitmap image) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        image.compress(Bitmap.CompressFormat.JPEG, 10, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
        int options = 100;
        while (baos.toByteArray().length / 1024 > 400) {  //循环判断如果压缩后图片是否大于400kb,大于继续压缩(这里可以设置大些)
            baos.reset();//重置baos即清空baos
            image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中
            options -= 10;//每次都减少10
        }
        ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中
        Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);//把ByteArrayInputStream数据生成图片
        return bitmap;
    }


3,把压缩过的图片先保存到本地才能调用系统分享出去,因为系统分享的是一个uri,我们需要先把bitmap转为本地file文件
再把file转换为uri
 /*
  * 把bitmap转化为file
  * */
    public File bitMap2File(Bitmap bitmap) {


        String path = "";
        if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
            path = Environment.getExternalStorageDirectory() + File.separator;//保存到sd根目录下
        }


        //        File f = new File(path, System.currentTimeMillis() + ".jpg");
        File f = new File(path, "share" + ".jpg");
        if (f.exists()) {
            f.delete();
        }
        try {
            FileOutputStream out = new FileOutputStream(f);
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
            bitmap.recycle();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            return f;
        }
    }

4,调用上面的方法获取到file,转换为uri并分享出去


File file = bitMap2File(compressImage);
if (file != null && file.exists() && file.isFile()) {
//由文件得到uri
        Uri imageUri = Uri.fromFile(file);
Intent shareIntent = new Intent();
shareIntent.setAction(Intent.ACTION_SEND);
shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
shareIntent.setType("image/*");
startActivity(Intent.createChooser(shareIntent, "分享图片"));
}



运用ComponentName来指定分享到哪里
1,指定是分享到微信好友
String imagePath = Environment.getExternalStorageDirectory() + File.separator +
                "huxiu.jpg";
//由文件得到uri
        Uri imageUri = Uri.fromFile(new File(imagePath));
        Intent shareIntent = new Intent();      
        //发送图片给好友。
        ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");
        shareIntent.setComponent(comp);
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
        shareIntent.setType("image/*");
        startActivity(Intent.createChooser(shareIntent, "分享图片"));

2,指定分享到朋友圈
String imagePath = Environment.getExternalStorageDirectory() + File.separator +
                "huxiu.jpg";
//由文件得到uri
        Uri imageUri = Uri.fromFile(new File(imagePath));
        Intent shareIntent = new Intent();      
        //发送图片到朋友圈
        ComponentName comp = new ComponentName("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");
        shareIntent.setComponent(comp);
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
        shareIntent.setType("image/*");
        startActivity(Intent.createChooser(shareIntent, "分享图片"));


3,指定分享到qq
String imagePath = Environment.getExternalStorageDirectory() + File.separator +
                "huxiu.jpg";
//由文件得到uri
        Uri imageUri = Uri.fromFile(new File(imagePath));
        Intent shareIntent = new Intent();      
        //发送图片到qq
        ComponentName comp = new ComponentName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");
        shareIntent.setComponent(comp);
        shareIntent.setAction(Intent.ACTION_SEND);
        shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
        shareIntent.setType("image/*");
        startActivity(Intent.createChooser(shareIntent, "分享图片"));

  1. Intent share = new Intent(android.content.Intent.ACTION_SEND);  
  2.         PackageManager packageManager = getPackageManager();  
  3.         List<ResolveInfo> list=packageManager.queryIntentActivities(share, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);  
  4.         for(ResolveInfo info:list){  
  5.             MyUtils.log(""+info.activityInfo.packageName+"---"+info.activityInfo.name);  
  6.         } 
遍历获取包名和类名:

  1. //        ("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");//微信朋友  
  2. //          ("com.tencent.mobileqq", "cooperation.qqfav.widget.QfavJumpActivity");//保存到QQ收藏  
  3. //         ("com.tencent.mobileqq", "cooperation.qlink.QlinkShareJumpActivity");//QQ面对面快传  
  4. //        ("com.tencent.mobileqq", "com.tencent.mobileqq.activity.qfileJumpActivity");//传给我的电脑  
  5.          ("com.tencent.mobileqq""com.tencent.mobileqq.activity.JumpActivity");//QQ好友或QQ群  
  6. //         ("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");//微信朋友圈,仅支持分享图片


功能不够强大

2.去各个平台注册自己的app,使用平台的分享协议

这种方式分享的内容丰富,而且弹出的分享界面正规无广告


3.直接使用第三方的ShareSdk重的分享(onekeyshare)

分享界面带有sharesdk的表示,很尴尬,但是简单

0 0
原创粉丝点击