public Bitmap createViewBitmap(View v) { Bitmap bitmap = Bitmap.createBitmap(1920, 1080, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); v.draw(canvas); return bitmap; } public int saveImageToGallery(Bitmap bmp) { //生成路径 String root = Environment.getExternalStorageDirectory().getAbsolutePath(); String dirName = "Screenshots"; File appDir = new File(root, dirName); if (!appDir.exists()) { appDir.mkdirs(); } //文件名为时间 long timeStamp = System.currentTimeMillis(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String sd = sdf.format(new Date(timeStamp)); String fileName = sd + ".jpg"; //获取文件 File file = new File(appDir, fileName); FileOutputStream fos = null; try { fos = new FileOutputStream(file); bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); //通知系统相册刷新 mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(file.getPath())))); return 2; } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fos != null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } } return -1; }
保存bitmap为图片
最新推荐文章于 2024-08-18 02:54:59 发布