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的表示,很尷尬,但是簡單
新聞熱點
疑難解答