android将view内容保存为图片在本地

Handler mHandler = new Handler();
/**
 * 生成二维码图片
 */
private void getShareImg() {
    cvLayout.setDrawingCacheEnabled(true);
    cvLayout.buildDrawingCache();
    mHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            // 要在运行在子线程中
            // 获取图片
            Bitmap bitmap = cvLayout.getDrawingCache();
            if (bitmap == null) {//处理华为meta9等手机出现的问题
                bitmap = Bitmap.createBitmap(cvLayout.getWidth(),
                        cvLayout.getHeight(), Bitmap.Config.ARGB_8888);
                Canvas canvas = new Canvas(bitmap);
                if (Build.VERSION.SDK_INT >= 11) {
                    cvLayout.measure(View.MeasureSpec.makeMeasureSpec(cvLayout.getWidth(),
                            View.MeasureSpec.EXACTLY), View.MeasureSpec.makeMeasureSpec(cvLayout.getHeight(), View.MeasureSpec.EXACTLY));
                    cvLayout.layout((int) cvLayout.getX(),
                            (int) cvLayout.getY(),
                            (int) cvLayout.getX() + cvLayout.getMeasuredWidth(),
                            (int) cvLayout.getY() + cvLayout.getMeasuredHeight());
                } else {
                    cvLayout.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
                    cvLayout.layout(0, 0, cvLayout.getMeasuredWidth(),
                            cvLayout.getMeasuredHeight());
                }
                cvLayout.draw(canvas);
            }
            savePicture(bitmap, "tuiguangerweima.jpg");// 保存图片
            cvLayout.destroyDrawingCache(); // 保存过后释放资源
        }
    }, 1000);
}

/** * 保存图片,并通知相册 */

public void savePicture(Bitmap bm, String fileName) {
    if (null == bm) {
        return;
    }
    File foder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/saojiajia");
    if (!foder.exists()) {
        foder.mkdirs();
    }
    File myCaptureFile = new File(foder, fileName);
    try {
        if (!myCaptureFile.exists()) {
            myCaptureFile.createNewFile();
        }
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
        //压缩保存到本地
        bm.compress(Bitmap.CompressFormat.JPEG, 90, bos);
        bos.flush();
        bos.close();
        toast("推广二维码已保存");
        getApplicationContext().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(myCaptureFile)));
        
    } catch (IOException e) {
        e.printStackTrace();
    }

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值