android 手机截取长屏实例代码
最近项目遇到一个需求:把当前页面保存到手机相册。想了想我还不会呢,就百度了下大神的足迹,踏着大神的足迹,一路向前。废话不说,记录下,后期学习。
publicclassScreenUtils{ /** *截取scrollview的屏幕 *@paramscrollView *@return */ publicstaticBitmapgetBitmapByView(ScrollViewscrollView){ inth=0; Bitmapbitmap=null; //获取scrollview实际高度 for(inti=0;i1024&&options>10){ //重置baos baos.reset(); //这里压缩options%,把压缩后的数据存放到baos中 image.compress(Bitmap.CompressFormat.JPEG,options,baos); //每次都减少10 options-=10; } //把压缩后的数据baos存放到ByteArrayInputStream中 ByteArrayInputStreamisBm=newByteArrayInputStream(baos.toByteArray()); //把ByteArrayInputStream数据生成图片 Bitmapbitmap=BitmapFactory.decodeStream(isBm,null,null); returnbitmap; } /** *保存到sdcard *@paramb *@return */ publicstaticStringsavePic(Contextcontext,Bitmapb){ Fileoutfile=newFile("/sdcard/image"); //如果文件不存在,则创建一个新文件 if(!outfile.isDirectory()){ try{ outfile.mkdir(); }catch(Exceptione){ e.printStackTrace(); } } Stringfname=outfile+"/"+System.currentTimeMillis()+".jpg"; FileOutputStreamfos=null; try{ fos=newFileOutputStream(fname); if(null!=fos){ b.compress(Bitmap.CompressFormat.JPEG,90,fos); fos.flush(); fos.close(); } }catch(FileNotFoundExceptione){ e.printStackTrace(); }catch(IOExceptione){ e.printStackTrace(); } //其次把文件插入到系统图库 try{ MediaStore.Images.Media.insertImage(context.getContentResolver(), outfile.getAbsolutePath(),fname,null); }catch(FileNotFoundExceptione){ e.printStackTrace(); } //最后通知图库更新 context.sendBroadcast(newIntent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,Uri.parse("file://"+fname))); returnfname; } }
以上为百度的工具类。
使用方法:
ScreenUtils .savePic(XXXActivity.this,ScreenUtils.compressImage(ScreenUtils .getBitmapByView(XXXScrollView)));
好了,截取成功了!
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。