【Arcgis for android】保存地图截图到sd卡
16lz
2021-01-23
关键词:arcgis for android ,截图,bitmap,sd卡
参考文章:http://blog.csdn.net/wozaifeiyang0/article/details/7679727
在arcgis for android地图中mapview加入截图功能。
参考上文,将mapview转化为bitmap。代码如下:
1 private Bitmap getViewBitmap(MapView v) { 2 v.clearFocus(); 3 v.setPressed(false); 4 5 //能画缓存就返回false 6 boolean willNotCache = v.willNotCacheDrawing(); 7 v.setWillNotCacheDrawing(false); 8 int color = v.getDrawingCacheBackgroundColor(); 9 v.setDrawingCacheBackgroundColor(0);10 if (color != 0) {11 v.destroyDrawingCache();12 }13 v.buildDrawingCache();14 Bitmap cacheBitmap = null;15 while(cacheBitmap == null){16 cacheBitmap = v.getDrawingMapCache(0, 0, v.getWidth(), v.getHeight());17 }18 Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);19 // Restore the view20 v.destroyDrawingCache();21 v.setWillNotCacheDrawing(willNotCache);22 v.setDrawingCacheBackgroundColor(color);23 return bitmap;24 }
然后处理存储的文件名以及保存到sd卡,此处采用日期加时间存储,精确到秒。
为了防止一秒内多次点击,文件名被占用,代码中加入了处理(虽然出现的概率比较小,但也是可能存在的。。。)。
1 private void mapviewshot() { 2 System.out.println("进入截屏方法"); 3 Date date=new Date(); 4 SimpleDateFormat dateformat1=new SimpleDateFormat("yyyyMMdd_hhmmss"); 5 String timeString=dateformat1.format(date); 6 String path="arcgis1/screenshot"; 7 String externalPath=Environment.getExternalStorageDirectory().toString(); 8 String filename=externalPath+"/"+path+"/"+timeString; 9 10 File file_2=new File(externalPath+"/"+path);11 if (!file_2.exists()){12 System.out.println("path 文件夹 不存在--开始创建");13 file_2.mkdirs();14 }15 filename=getfilepath(filename);//判断是否有同一秒内的截图,有就改名字16 //存储于sd卡上17 System.out.println("获得的filename--"+filename);18 Bitmap bitmap=getViewBitmap(mMapView);19 20 File file=new File(filename); 21 try {22 FileOutputStream fileOutputStream=new FileOutputStream(file);23 bitmap.compress(Bitmap.CompressFormat.PNG, 90, fileOutputStream);24 fileOutputStream.flush();25 fileOutputStream.close();26 } catch (FileNotFoundException e) {27 e.printStackTrace();28 } catch (IOException e) {29 e.printStackTrace();30 }31 32 33 }34 private String getfilepath(String filename) {35 String filestr=filename+".png";36 File file=new File(filestr);37 if (file.exists()){38 filename=getfilepath(filename+"_1");39 }40 else {41 filename=filestr;42 }43 System.out.println("getfilename函数返回----"+filename);44 return filename;45 }
还可以处理的是加入命名的对话框,实现自由命名。textview固定文件夹路径,提供textfield供用户命名,然后保存。
更多相关文章
- android 通过资源文件名称获取资源文件id
- [android]android命令行截图
- Android Studio 3.0以后打包修改文件名方法
- android webview 截图快照
- Android Studio设置apk文件名
- android Gradle打包修改生成的apk文件名称
- Android 屏幕截图并用WindowManager显示
- 微软的 Android 计划:邪恶的天才计划或只是邪恶?