国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

分享功能的實現方式

2019-11-09 17:18:12
字體:
來源:轉載
供稿:網友

1.通過intent調用系統(tǒng)中自帶的分享功能。

    //分享文字      publicvoid shareText(View view) {          Intent shareIntent = newIntent();          shareIntent.setAction(Intent.ACTION_SEND);          shareIntent.putExtra(Intent.EXTRA_TEXT, "描述信息" + "這里你可以追加一個url連接"););          shareIntent.setType("text/plain");             //設置分享列表的標題,并且每次都顯示分享列表          startActivity(Intent.createChooser(shareIntent,"分享到"));      }         //分享單張圖片      publicvoid shareSingleImage(View view) {          String imagePath = Environment.getExternalStorageDirectory() + File.separator + "test.jpg";          //由文件得到uri          Uri imageUri = Uri.fromFile(newFile(imagePath));          Log.d("share","uri:"+ imageUri);  //輸出:file:///storage/emulated/0/test.jpg             Intent shareIntent = newIntent();          shareIntent.setAction(Intent.ACTION_SEND);          shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);          shareIntent.setType("image/*");          startActivity(Intent.createChooser(shareIntent,"分享到"));      }         //分享多張圖片      publicvoid shareMultipleImage(View view) {          ArrayList<uri> uriList = newArrayList<>();             String path = Environment.getExternalStorageDirectory() + File.separator;          uriList.add(Uri.fromFile(newFile(path+"australia_1.jpg")));          uriList.add(Uri.fromFile(newFile(path+"australia_2.jpg")));          uriList.add(Uri.fromFile(newFile(path+"australia_3.jpg")));             Intent shareIntent = newIntent();          shareIntent.setAction(Intent.ACTION_SEND_MULTIPLE);          shareIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uriList);          shareIntent.setType("image/*");          startActivity(Intent.createChooser(shareIntent,"分享到"));      }  通常我們分享的圖片不可能是本地存好的圖片,我們通常分享的是自己生成的圖片獲者網絡圖片這里簡單給大家介紹一個仿好奇心日報那樣的把本地布局截圖分享出去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) {  //循環(huán)判斷如果壓縮后圖片是否大于400kb,大于繼續(xù)壓縮(這里可以設置大些)            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,把壓縮過的圖片先保存到本地才能調用系統(tǒng)分享出去,因為系統(tǒng)分享的是一個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();              //發(fā)送圖片給好友。        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();              //發(fā)送圖片到朋友圈        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();              //發(fā)送圖片到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, "分享圖片"));

Intent share = new Intent(android.content.Intent.ACTION_SEND);          PackageManager packageManager = getPackageManager();          List<ResolveInfo> list=packageManager.queryIntentActivities(share, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT);          for(ResolveInfo info:list){              MyUtils.log(""+info.activityInfo.packageName+"---"+info.activityInfo.name);          }  遍歷獲取包名和類名:

//        ("com.tencent.mm", "com.tencent.mm.ui.tools.ShareImgUI");//微信朋友  //          ("com.tencent.mobileqq", "coOperation.qqfav.widget.QfavJumpActivity");//保存到QQ收藏  //         ("com.tencent.mobileqq", "cooperation.qlink.QlinkShareJumpActivity");//QQ面對面快傳  //        ("com.tencent.mobileqq", "com.tencent.mobileqq.activity.qfileJumpActivity");//傳給我的電腦           ("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");//QQ好友或QQ群  //         ("com.tencent.mm", "com.tencent.mm.ui.tools.ShareToTimeLineUI");//微信朋友圈,僅支持分享圖片

功能不夠強大

2.去各個平臺注冊自己的app,使用平臺的分享協議

這種方式分享的內容豐富,而且彈出的分享界面正規(guī)無廣告

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

分享界面帶有sharesdk的表示,很尷尬,但是簡單


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 台东市| 龙井市| 黔南| 易门县| 抚顺市| 喀喇沁旗| 公安县| 观塘区| 肥东县| 淮滨县| 翼城县| 平凉市| 金溪县| 岳西县| 库车县| 达州市| 景谷| 海伦市| 文安县| 广德县| 遵义县| 五华县| 邓州市| 龙胜| 扎赉特旗| 阜新| 灵寿县| 东港市| 崇礼县| 靖安县| 临漳县| 江陵县| 定兴县| 土默特左旗| 偏关县| 开江县| 镇雄县| 离岛区| 重庆市| 江西省| 涪陵区|