Android布局生成图片并保存

     Bitmap bmp = Bitmap.createBitmap(rl.getWidth(), rl.getHeight(), Bitmap.Config.ARGB_8888);
     rl.draw(new Canvas(bmp));
     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
     String time = dateFormat.format(new Date());

     String path = Environment.getExternalStorageDirectory().getPath() + "/doutu/";
     File dir = new File(path);
     if (!dir.exists()) {
         dir.mkdirs();
     }

     String photoUrl = path + time + ".png";//换成自己的图片保存路径

     File file = new File(photoUrl);
     try {
         stream = new FileOutputStream(file);
         compress = bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
     } catch (FileNotFoundException e) {
         e.printStackTrace();
     }
     if (compress) {
         sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
         Toast.makeText(getApplicationContext(), "保存到:" + file, Toast.LENGTH_SHORT).show();
     }

不写这句系统图库看不到保存的图片
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));**

你可能感兴趣的:(图片)