zoukankan      html  css  js  c++  java
  • 【Arcgis for android】保存地图截图到sd卡

    关键词: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 view
    20         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供用户命名,然后保存。

  • 相关阅读:
    python之字典dict
    python之 tuple元组
    python之列表list
    数字图像处理
    深度神经网络优化
    神经网络的前向后向及更新
    0220 kd树(鸢尾花分类)
    024 查看数据库的编码格式
    208 MySQL性能分析之Explain
    207 MySQL索引的数据结构B+树介绍
  • 原文地址:https://www.cnblogs.com/wangcan/p/3457914.html
Copyright © 2011-2022 走看看