我正在尝试在Android平台上导入.geodatabase文件,但是很难这样做。
public void onRadioButtonClicked(View view) {
map = (MapView) findViewById(R.id.map);
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio1:
if (checked)
Toast.makeText(this, "You've selected: Roads",Toast.LENGTH_LONG).show();
try{
//Open the geodatabase file
Toast.makeText(this, "before geodatabase", Toast.LENGTH_LONG).show();
Geodatabase geodatabase = new Geodatabase("/new_file_geodatabase.geodatabase");
//private GeodatabaseFeatureTable geodatabaseFeatureTable;
//get the geodatabase feature table
geodatabaseFeatureTable = geodatabase.getGdbFeatureTableByLayerId(0);
//create a feature layer
Toast.makeText(this, "before again", Toast.LENGTH_LONG).show();
featureLayer = new FeatureLayer(geodatabaseFeatureTable);
map.addLayer(featureLayer);
Toast.makeText(this, "after geo", Toast.LENGTH_LONG).show();
} catch(Exception e){
e.printStackTrace();
Toast.makeText(this, "catch", Toast.LENGTH_LONG).show();
}
Toast.makeText(this, "Roads loaded", Toast.LENGTH_LONG).show();
break;
case R.id.radio2:
if (checked)
Toast.makeText(this, "You've selected: Villages and Banks", Toast.LENGTH_LONG).show();
local = new ArcGISLocalTiledLayer("file:///mnt/sdcard/vill n bnk.tpk");
map.addLayer(local);
Toast.makeText(this, "Villages and Banks Loaded", Toast.LENGTH_LONG).show();
break;
}
}
如果你可以在第一个radiobutton点击选项中看到,在try块内我已经放了3个toast消息来检查程序的流程并注意到在显示"之前的GEODATABASE"吐司流控制进入catch块。我猜密码消息后面的代码(在try块内部不起作用)。
我很确定使用xml文件,android清单文件和其他支持文件的所有内容都很好。
我能否就代码的错误提出任何建议。
答案 0 :(得分:0)
Geodatabase geodatabase = new Geodatabase("/new_file_geodatabase.geodatabase");
我非常怀疑您是否设法将new_file_geodatabase.geodatabase
放入Android的根目录/
中。如果你这样做,我向你致敬。如果没有,您需要使用.geodatabase文件的绝对路径。
最有可能的是,Geodatabase(String)
constructor正在宣传FileNotFoundException
。
如果要验证该文件不存在,请尝试将其作为测试:
File f = new File("/new_file_geodatabase.geodatabase");
Log.d(getClass().getSimpleName(), "Does the file exist? " + f.exists());
检查日志。它将打印“false”,因为/new_file_geodatabase.geodatabase
不存在。您的文件可能位于/mnt/sdcard
或/mnt/extSdCard
或其他位置,但不在/
。
答案 1 :(得分:0)
我尝试了几种代码组合来完成任务...... 附在下面
1.不使用网络概念的简单代码
这个读取错误:数据库文件无法打开
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeRoutingAndGeocoding();
map = (MapView) findViewById(R.id.map);
local = new ArcGISLocalTiledLayer("file:///mnt/sdcard/SanDiego.tpk");
map.addLayer(local);
}
private void initializeRoutingAndGeocoding() {
Toast.makeText(this, "in initialize and geo", Toast.LENGTH_LONG).show();
//String networkPath = "/RuntimeSanDiego.geodatabase";
//String networkName = "Streets_ND";
Toast.makeText(getApplicationContext(), "after files initied", Toast.LENGTH_LONG).show();
try {
Toast.makeText(getApplicationContext(), "in try", Toast.LENGTH_LONG).show();
Geodatabase geodatabase = new Geodatabase("file:///mnt/sdcard/RuntimeSanDiego.geodatabase");
geodatabaseFeatureTable = geodatabase.getGdbFeatureTableByLayerId(0);
featureLayer = new FeatureLayer(geodatabaseFeatureTable);
map.addLayer(featureLayer);
Toast.makeText(getApplicationContext(), "success", Toast.LENGTH_LONG).show();
} catch (Exception e) {
popToast("Error while initializing :" + e.getMessage(),true);
e.printStackTrace();
Toast.makeText(getApplicationContext(), "catch", Toast.LENGTH_LONG).show();
}
}
2.使用网络文件完美无缺。没有错误
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeRoutingAndGeocoding();
map = (MapView) findViewById(R.id.map);
local = new ArcGISLocalTiledLayer("file:///mnt/sdcard/SanDiego.tpk");
map.addLayer(local);
}
private void initializeRoutingAndGeocoding() {
Toast.makeText(this, "in initialize and geo", Toast.LENGTH_LONG).show();
String networkPath = "/RuntimeSanDiego.geodatabase";
String networkName = "Streets_ND";
Toast.makeText(getApplicationContext(), "after files initied", Toast.LENGTH_LONG).show();
try {
Toast.makeText(getApplicationContext(), "in try", Toast.LENGTH_LONG).show();
mRouteTask = RouteTask.createLocalRouteTask(extern
+ networkPath, networkName);
Toast.makeText(getApplicationContext(), "success", Toast.LENGTH_LONG).show();
} catch (Exception e) {
popToast("Error while initializing :" + e.getMessage(),true);
e.printStackTrace();
Toast.makeText(getApplicationContext(), "catch", Toast.LENGTH_LONG).show();
}
}
3.只是清楚地传达给您的信息。我使用了此示例file:///mnt/sdcard/RuntimeSanDiego.geodatabase
中的完整文件地址
而在前一个我没有。
此处错误显示:无法打开网络
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeRoutingAndGeocoding();
map = (MapView) findViewById(R.id.map);
local = new ArcGISLocalTiledLayer("file:///mnt/sdcard/SanDiego.tpk");
map.addLayer(local);
}
private void initializeRoutingAndGeocoding() {
Toast.makeText(this, "in initialize and geo", Toast.LENGTH_LONG).show();
String networkPath = "file:///mnt/sdcard/RuntimeSanDiego.geodatabase";
String networkName = "Streets_ND";
Toast.makeText(getApplicationContext(), "after files initied", Toast.LENGTH_LONG).show();
try {
Toast.makeText(getApplicationContext(), "in try", Toast.LENGTH_LONG).show();
mRouteTask = RouteTask.createLocalRouteTask(extern
+ networkPath, networkName);
Toast.makeText(getApplicationContext(), "success", Toast.LENGTH_LONG).show();
} catch (Exception e) {
popToast("Error while initializing :" + e.getMessage(),true);
e.printStackTrace();
Toast.makeText(getApplicationContext(), "catch", Toast.LENGTH_LONG).show();
}
}
4.尝试过这么多组合我怀疑网络名称在处理地理数据库文件时也会计算。我将网络名称从Streets_ND
更改为abcd
,但令人惊讶的是,此处的代码无效。
错误内容为:无法打开网络
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeRoutingAndGeocoding();
map = (MapView) findViewById(R.id.map);
local = new ArcGISLocalTiledLayer("file:///mnt/sdcard/SanDiego.tpk");
map.addLayer(local);
}
private void initializeRoutingAndGeocoding() {
Toast.makeText(this, "in initialize and geo", Toast.LENGTH_LONG).show();
String networkPath = "/RuntimeSanDiego.geodatabase";
String networkName = "abcd";
Toast.makeText(getApplicationContext(), "after files initied", Toast.LENGTH_LONG).show();
try {
Toast.makeText(getApplicationContext(), "in try", Toast.LENGTH_LONG).show();
mRouteTask = RouteTask.createLocalRouteTask(extern
+ networkPath, networkName);
Toast.makeText(getApplicationContext(), "success", Toast.LENGTH_LONG).show();
} catch (Exception e) {
popToast("Error while initializing :" + e.getMessage(),true);
e.printStackTrace();
Toast.makeText(getApplicationContext(), "catch", Toast.LENGTH_LONG).show();
}
}
5.最后,对样本数据进行了实验(从https://developers.arcgis.com/android/sample-code/offline-routing/
下载)尝试在代码中运行我自己的数据。
错误内容为:无法打开网络 但是road.tpk文件显示在模拟器上。
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeRoutingAndGeocoding();
map = (MapView) findViewById(R.id.map);
local = new ArcGISLocalTiledLayer("file:///mnt/sdcard/road.tpk");
map.addLayer(local);
}
private void initializeRoutingAndGeocoding() {
Toast.makeText(this, "in initialize and geo", Toast.LENGTH_LONG).show();
String networkPath = "/new_file_geodatabase.geodatabase";
String networkName = "Streets_ND";
Toast.makeText(getApplicationContext(), "after files initied", Toast.LENGTH_LONG).show();
try {
Toast.makeText(getApplicationContext(), "in try", Toast.LENGTH_LONG).show();
mRouteTask = RouteTask.createLocalRouteTask(extern
+ networkPath, networkName);
Toast.makeText(getApplicationContext(), "success", Toast.LENGTH_LONG).show();
} catch (Exception e) {
popToast("Error while initializing :" + e.getMessage(),true);
e.printStackTrace();
Toast.makeText(getApplicationContext(), "catch", Toast.LENGTH_LONG).show();
}
}