问题描述:shapefileTable 始终为null,shpName值为空,geometryType 为unknown,无法加载shapefile
ShapefileFeatureTable shapefileTable = new ShapefileFeatureTable(mappath);
shapefileTable.loadAsync();
String shpName = shapefileTable.getTableName();//获取shp文件名
GeometryType geometryType = shapefileTable.getGeometryType();//获取shp的类型
全文代码
public class MainActivity extends AppCompatActivity {
String shpRootPath1 = "map/FW_NEW.shp";
private String shpPath = null;
private MapView mapview = null;
private static final int READ_EXTERNAL_STORAGE= 1;//读取文件权限
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mapview = findViewById(R.id.mapView);
Button loadShpBtn = findViewById(R.id.loadShp);
OpenStreetMapLayer openStreetMapLayer = new OpenStreetMapLayer();
Basemap basemap = new Basemap(openStreetMapLayer);
ArcGISMap arcGISMap = new ArcGISMap(basemap);
mapview.setMap(arcGISMap);
//查看权限
if (ContextCompat.checkSelfPermission(
MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE)!=PackageManager.PERMISSION_GRANTED){
ActivityCompat.requestPermissions(MainActivity.this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,Manifest.permission.WRITE_EXTERNAL_STORAGE},READ_EXTERNAL_STORAGE);
}else {
shpPath = getSDCardPath() + shpRootPath1;
File file = new File(shpPath);
if (file.exists()){
showShapefile(shpPath,mapview);
}else {
Toast.makeText(this,"文件不存在",Toast.LENGTH_SHORT).show();
}
}
}
/**
* 加载shp文件
* @param mappath
* @param mapView
*/
public void showShapefile(String mappath,MapView mapView) {
ShapefileFeatureTable shapefileTable = new ShapefileFeatureTable(mappath);
shapefileTable.loadAsync();
String shpName = shapefileTable.getTableName();//获取shp文件名
GeometryType geometryType = shapefileTable.getGeometryType();//获取shp的类型
if (geometryType == GeometryType.UNKNOWN){
shapefileTable.addDoneLoadingListener(()-> {
FeatureLayer shapefileLayer = new FeatureLayer(shapefileTable);
if (shapefileLayer.getFullExtent() != null) {
mapView.setViewpointGeometryAsync(shapefileLayer.getFullExtent());
} else
{
shapefileLayer.addDoneLoadingListener(()->
mapView.setViewpointGeometryAsync(shapefileLayer.getFullExtent()));
}
SimpleLineSymbol lineSymbol = new SimpleLineSymbol(SimpleLineSymbol.Style.SOLID, R.color.red, 1.0f);
SimpleFillSymbol fillSymbol = new SimpleFillSymbol(SimpleFillSymbol.Style.NULL, R.color.red, lineSymbol);
SimpleRenderer renderer = new SimpleRenderer(fillSymbol);
shapefileLayer.setRenderer(renderer);
mapView.getMap().getOperationalLayers().add(shapefileLayer);
});
}else{
Toast.makeText(MainActivity.this,"加载失败",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode){
case READ_EXTERNAL_STORAGE:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
//开始操作
shpPath = getSDCardPath() + shpRootPath1;
File file = new File(shpPath);
if (file.exists()){
showShapefile(shpPath,mapview);
}else {
System.out.println("不存在");
}
}else{
Toast.makeText(this,"你取消了授权",Toast.LENGTH_LONG).show();
}
break;
default:
break;
}
}
// 获取SD卡路径
public String getSDCardPath()
{
return Environment.getExternalStorageDirectory().getAbsolutePath()
+ File.separator;
}
}
权限界面
shapefile文件存放路径