本文主要介绍在代码工程中添加数据并部署到移动终端的操作步骤
数据是GIS应用的基础,与所有GIS应用一样,用 ArcGIS for Android 开发的应用也离不开数据。而Android应用是在移动终端上运行的,数据和应用安装包都需要通过数据线连接进行拷贝,当终端较多时,尤其显得数据拷贝工作既繁琐又枯燥。为了更方便地部署应用数据,可以将数据文件放在工程资源中,在程序启动的同时,将数据一起部署到目标设备的指定目录。
本文介绍在代码工程中添加数据并部署到移动终端的操作步骤:
首先,数据文件拷贝到工程的assets目录;
然后,程序启动时,获取assets的内容并将其存储至移动端的指定目录。通过assets的方式,可以将数据一起打包到apk安装包中,安装完成后,程序启动时即可将数据部署至移动端,不需要再单独拷贝。
代码如下:
public void copyAssetsDirToSdcard(String dir) {
String desFolder = ZZMANHOLE_PATH + dir; FileUtils.createOrExistsDir(desFolder);
String[] mAssetsFileList = null; try { mAssetsFileList = getAssets().list(dir); } catch (IOException e) { e.printStackTrace(); }
assert mAssetsFileList != null; for (String file : mAssetsFileList) { File desFile = new File(desFolder + "/" + file); if (!desFile.exists()) try { InputStream is = getAssets().open(dir + "/" + file); writeFileFromIS(desFile, is, false);
} catch (IOException e) { e.printStackTrace(); } } }
|
说明:
1、assets 目录位置 \app\src\main\assets;
2、其中 ZZMANHOLE_PATH 为项目目录可通过以下代码获得:
public static String PATH = android.os.Environment.getExternalStorageDirectory() + "/pathname/";
|
3、将 assets 目录下的 path 复制到 SDcard 项目目录:
copyAssetsDirToSdcard(“path”);
即将\app\src\main\assets\path 复制到 PATH\path;
4、writeFileFromIS 代码如下:
public static boolean writeFileFromIS(File file, InputStream is, boolean append) { if (file == null || is == null) return false; if (!createOrExistsFile(file)) return false; OutputStream os = null; try { os = new BufferedOutputStream(new FileOutputStream(file, append)); byte data[] = new byte[1024]; int len; while ((len = is.read(data, 0, 1024)) != -1) { os.write(data, 0, len); } return true; } catch (IOException e) { e.printStackTrace(); return false; } finally { closeIO(is, os); } }
|
5、closeIO 代码如下:
public static void closeIO(Closeable... closeables) { if (closeables == null) return; for (Closeable closeable : closeables) { if (closeable != null) { try { closeable.close(); } catch (IOException e) { e.printStackTrace(); } } } }
|
参考资料
1、java - android 怎么复制assets文件夹到本地SD卡? - SegmentFault https://segmentfault.com/q/1010000004478829
2、Android学习之遍历拷贝assets下的目录 - linzhiyong的专栏 - 博客频道 - CSDN.NET http://blog.csdn.net/u012527802/article/details/52025849