美文网首页
Android 页面截图实现

Android 页面截图实现

作者: 孤街酒客0911 | 来源:发表于2023-06-14 19:26 被阅读0次

学习笔记:又转行了,从系统转到应用啦


    public Bitmap createViewBitmap(View view) {
        // 传入一个view,这个view就是要截的视图
        Bitmap bitmap = null;
        try {
            // 调用系统的方法 createBitmap 得到 bitmap
            bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            // 设置背景颜色
            canvas.drawColor(getResources().getColor(R.color.color_e9e7ef));
            view.draw(canvas);
        } catch (Exception e) {
            LogUtils.log("he", e.getMessage());
        }

        return bitmap;
    }

调用如下方法,传入 bitmap 即可;

   public String saveBitmapr(Context context, Bitmap bmp) {
        // 首先保存图片
        File appDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "demo");
        if (!appDir.exists()) {
            appDir.mkdir();
        }
        String fileName = System.currentTimeMillis() + ".jpg";
        File file = new File(appDir, fileName);
        try {
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.flush();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("------保存图片失败-------"+e);
            return "";
        }

        // 最后通知图库更新
        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file)));
        return file.getAbsolutePath();

    }

相关文章

网友评论

      本文标题:Android 页面截图实现

      本文链接:https://www.haomeiwen.com/subject/ivihydtx.html