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

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

作者: IT烟酒僧 | 来源:发表于2018-07-17 20:49 被阅读204次

    将布局文件转化成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;
        } 
    

    相关文章

      网友评论

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

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