在上篇文章給大家介紹了Android實現截圖和分享功能的代碼。感興趣可以點擊閱讀,今天通過本文給大家介紹Android實現截圖分享qq 微信功能。一起看看吧。
前言
現在很多應用都有截圖分享的功能,今天就來講講截圖分享吧
今天涉及到以下內容:
ok,下面就來具體講講
一.權限,注意權限
先在自己的mainfast中添加以下權限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
然后是要申請Android7.0以上的權限,之前講過了,這里就不再廢話了。
二.截圖分享類
代碼如下:
package com.dialogfragmentdemo.util; import android.content.Context; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import java.io.File; import java.io.FileOutputStream; /** * Title:截屏分享 * Description: * 需要用戶讀寫權限 * <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> * <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> * * Created by pei * Date: 2017/12/6 */ public class ShotShareUtil { /**截屏分享,供外部調用**/ public static void shotShare(Context context){ //截屏 String path=screenShot(context); //分享 if(StringUtil.isNotEmpty(path)){ ShareImage(context,path); } } /**獲取截屏**/ private static String screenShot(Context context){ String imagePath=null; Bitmap bitmap= ScreenUtil.snapShotWithoutStatusBar(context); if(bitmap!=null){ try { // 圖片文件路徑 imagePath = SDCardUtil.getDiskCachePath()+"share.png"; LogUtil.e(ShotShareUtil.class, "====imagePath====" + imagePath); File file = new File(imagePath); FileOutputStream os = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.PNG, 100, os); os.flush(); os.close(); return imagePath; } catch (Exception e) { LogUtil.e(ShotShareUtil.class, "====screenshot:error====" + e.getMessage()); } } return null; } /**分享**/ private static void ShareImage(Context context,String imagePath){ if (imagePath != null){ Intent intent = new Intent(Intent.ACTION_SEND); // 啟動分享發送的屬性 File file = new File(imagePath); intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));// 分享的內容 intent.setType("image/*");// 分享發送的數據類型 Intent chooser = Intent.createChooser(intent, "Share screen shot"); if(intent.resolveActivity(context.getPackageManager()) != null){ context.startActivity(chooser); } } else { ToastUtil.shortShow("先截屏,再分享"); } } }
三.在mainactivity中調用
以下是示例代碼:
@Override public void onClick(View v) { super.onClick(v); switch (v.getId()) { case R.id.button: LogUtil.e(MainActivity.class,"====我點擊了===="); //截屏分享 ShotShareUtil.shotShare(mContext); break; default: break; } }
四.效果圖
上面是分享的時候,手機上沒裝qq和微信的情況,下面展示有qq,微信的情況
總結
以上所述是小編給大家介紹的Android實現截圖分享qq 微信功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。