【Arcgis for android】保存地图截图到sd卡

时间:2022-08-29 21:05:44

关键词:arcgis for android ,截图,bitmap,sd卡

参考文章:http://blog.csdn.net/wozaifeiyang0/article/details/7679727

在arcgis for android地图中mapview加入截图功能。

参考上文,将mapview转化为bitmap。代码如下:

 private Bitmap getViewBitmap(MapView v) {
v.clearFocus();
v.setPressed(false); //能画缓存就返回false
boolean willNotCache = v.willNotCacheDrawing();
v.setWillNotCacheDrawing(false);
int color = v.getDrawingCacheBackgroundColor();
v.setDrawingCacheBackgroundColor(0);
if (color != 0) {
v.destroyDrawingCache();
}
v.buildDrawingCache();
Bitmap cacheBitmap = null;
while(cacheBitmap == null){
cacheBitmap = v.getDrawingMapCache(0, 0, v.getWidth(), v.getHeight());
}
Bitmap bitmap = Bitmap.createBitmap(cacheBitmap);
// Restore the view
v.destroyDrawingCache();
v.setWillNotCacheDrawing(willNotCache);
v.setDrawingCacheBackgroundColor(color);
return bitmap;
}

然后处理存储的文件名以及保存到sd卡,此处采用日期加时间存储,精确到秒。

为了防止一秒内多次点击,文件名被占用,代码中加入了处理(虽然出现的概率比较小,但也是可能存在的。。。)。

 private void mapviewshot() {
System.out.println("进入截屏方法");
Date date=new Date();
SimpleDateFormat dateformat1=new SimpleDateFormat("yyyyMMdd_hhmmss");
String timeString=dateformat1.format(date);
String path="arcgis1/screenshot";
String externalPath=Environment.getExternalStorageDirectory().toString();
String filename=externalPath+"/"+path+"/"+timeString; File file_2=new File(externalPath+"/"+path);
if (!file_2.exists()){
System.out.println("path 文件夹 不存在--开始创建");
file_2.mkdirs();
}
filename=getfilepath(filename);//判断是否有同一秒内的截图,有就改名字
//存储于sd卡上
System.out.println("获得的filename--"+filename);
Bitmap bitmap=getViewBitmap(mMapView); File file=new File(filename);
try {
FileOutputStream fileOutputStream=new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 90, fileOutputStream);
fileOutputStream.flush();
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} }
private String getfilepath(String filename) {
String filestr=filename+".png";
File file=new File(filestr);
if (file.exists()){
filename=getfilepath(filename+"_1");
}
else {
filename=filestr;
}
System.out.println("getfilename函数返回----"+filename);
return filename;
}

还可以处理的是加入命名的对话框,实现*命名。textview固定文件夹路径,提供textfield供用户命名,然后保存。