温馨提示:我这里分享使用的是极光的Jshare,也可以使用原生分享
这里是JshareSDK接入的链接:链接: https://docs.jiguang.cn/jshare/client/Android/android_sdk
//扫描二维码
implementation 'com.github.yuzhiqiang1993:zxing:2.2.9'
//生成二维码
Bitmap logo = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);//二维码中心logo
Bitmap qrCode = CodeCreator.createQRCode(url, 400, 400, logo);
ImageView codeImg = view.findViewById(R.id.scan_code);
if (qrCode != null) {
codeImg.setImageBitmap(qrCode);
}
回归正题 截取view
1、获取需要截取的View 这里是弹窗中需要截取的view
/**
*dialog中的view获取和回调
*/
LinearLayout screenshotView = view.findViewById(R.id.screenshot_view);//截取的视图
//弹窗按钮点击截取 回传view
button.setOnClickListener(new View.OnClickListener() {
@SingleClick
@Override
public void onClick(View v) {
mListener.onSaveAndShare(screenshotView);//需要回传的view
}
});
/**
*页面中的dialog回调
*/
private View screenshotView;
scanningDialog.setOnShareListener(new ScanningDialog.OnShareListener() {
@Override
public void oncLoseDialog() {
scanningDialog.dismiss();
}
@Override
public void onSaveAndShare(View view) {
//保存图片并分享
screenshotView = view;
screenShotView();
}
});
2、dialog弹窗截屏
private final int REQUEST_STORAGE = 10223;//存储权限
/**
* dialog弹窗截屏
* 最终调用方法
*/
public void screenShotView() {
//动态获取权限
if (checkPermission()) {
saveShotView();//截取图片
} else {
requestPermission();//权限请求
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == REQUEST_STORAGE) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
saveShotView();//截取图片
} else {
requestPermission();//权限请求
}
}
}
3、获取权限 checkPermission() requestPermission()
/**
* check权限
* request权限
*/
private boolean checkPermission() {
int selfPermission = ContextCompat.checkSelfPermission(requireContext(), Manifest.permission.WRITE_EXTERNAL_STORAGE);
return selfPermission == PackageManager.PERMISSION_GRANTED;
}
private void requestPermission() {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_STORAGE);
}
4、截取并分享 saveShotView()
/**
* 截取图片并保存 可截取任意视图
* screenshotView需要截取的view 第一步中的视图对象
*/
private void saveShotView() {
String CAMERA_DIR = Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/Camera";
Bitmap tempBitmap = Bitmap.createBitmap(screenshotView.getWidth(), screenshotView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(tempBitmap);
screenshotView.draw(canvas);
File fileDir = new File(CAMERA_DIR);
if (!fileDir.exists()) {
boolean mkdirs = fileDir.mkdirs();
}
String path = CAMERA_DIR + "/share_screen.png";
try {
FileOutputStream foStream = new FileOutputStream(path);
tempBitmap.compress(Bitmap.CompressFormat.PNG, 100, foStream);
foStream.flush();
foStream.close();
if (scanningDialog != null) {
scanningDialog.dismiss();
}
ToastUtils.toast("保存成功,请在相册中查看");
shareNowImage(path);//todo调用分享
} catch (Exception e) {
Log.e("保存图片出错", e.toString());
}
}
5、极光分享 shareNowImage(String path)
/**
* path 本地图片路径
* shareListener分享回调
*/
private void shareNowImage(String path) {
List<String> platforms = JShareInterface.getPlatformList();
PageEventUtils.inFriendsNow(requireContext());
ShareParams shareParams = new ShareParams();
shareParams.setShareType(Platform.SHARE_IMAGE);
shareParams.setImagePath(path);
JShareInterface.share(platforms.get(0), shareParams, shareListener);
}
//分享回调
private final PlatActionListener shareListener = new PlatActionListener() {
@Override
public void onComplete(Platform platform, int action, HashMap<String, Object> data) {
ToastUtils.toast("分享成功");
}
@Override
public void onError(Platform platform, int action, int errorCode, Throwable error) {
Log.e("分享失败", "error:" + errorCode + ",msg:" + error);
}
@Override
public void onCancel(Platform platform, int action) {
ToastUtils.toast("取消分享");
}
};