将布局文件转化成Bitmap图片并保存到本地

将布局文件转化成Bitmap图片并保存到本地

  • 获取windownwidth

      int windowWidth = MyApplication.getWindowWidth();
    
  • 将布局文件转化成Bitmap

      public Bitmap getScrollViewBitmap(RelativeLayout  relativeLayout) {
       int h = 0;
       Bitmap bitmap;
       h = relativeLayout.getHeight();
       bitmap = Bitmap.createBitmap(windowWidth, h, Bitmap.Config.ARGB_4444);
       final Canvas canvas = new Canvas(bitmap);
       canvas.drawColor(Color.parseColor("#f2f7fa"));
       relativeLayout.draw(canvas);
       return bitmap;
      }
    
  • 保存生成的Bitmap图片到本地

    public String saveImageToGallery(Context context, Bitmap bmp) {
        String path = Environment.getExternalStorageDirectory() + "/DAYU2/Screen";
        File fileFolder = new File(path);
        if (!fileFolder.exists()) {
          fileFolder.mkdirs();
        }
        String fileName = "Screen_" + System.currentTimeMillis() + ".jpg";
        File file = new File(fileFolder, fileName);
        try {
          FileOutputStream fos = new FileOutputStream(file);
          bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
          fos.flush();
          fos.close();
        } catch (FileNotFoundException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
        return file.getAbsolutePath();
    }
  • 获取本地保存的Bitmap地址
    public String shareBitmap() {
       Bitmap xxBitmap = getScrollViewBitmap(comPictureOutRl);
       if (xxBitmap == null) {
         return null;
       }
       String s = saveImageToGallery(getContext(), xxBitmap);
       return s;
    } 
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容