How to read shape file from sd card on Android?

2284
2
11-21-2016 04:37 AM
CrisPlutis
New Contributor
Hello,

I was trying to display some geometry from a shapefile in Android and I ran into this exception when trying to create a 
ShapeFileFeatureTable using a path on the sd card:

 java.lang.RuntimeException: Shape file not found: /storage/emulated/0/stations.shp
 at com.esri.core.geodatabase.Geodatabase.nativeOpenShapefile(Native Method)
 at com.esri.core.geodatabase.Geodatabase.a(SourceFile:126)
 at com.esri.core.geodatabase.ShapefileFeatureTable.<init>(SourceFile:79)

I've tried with different paths and with and without the first '/' and haven't managed to fix this error. The file exists on the 
sd card and can be read by the app as Geodatabase.a() checks if the file exists and throws a FileNotFoundException otherwise.

This is the source code I'm using (the full project and shape file are also attached):

File esd = Environment.getExternalStorageDirectory();
//        String path = esd.getPath() + "/stations.shp";
String path = "/storage/emulated/0/stations.shp";
Log.w("shp path", "path " + path);
ShapefileFeatureTable shpFile = null;
try {
   shpFile = new ShapefileFeatureTable(path);
   if (shpFile != null) {
       Envelope env = shpFile.getExtent();
       if (env != null) {
           Log.w("read shape", env.getWidth() + "x" + env.getHeight());
       }
   }
} catch (FileNotFoundException e) {
    e.printStackTrace();
}

Can anyone please tell me if i'm doing something wrong or if the shape file is not valid. Also is there any other way to display 
Geometry objects from shape files? Any help will be appreciated.

Thank you,
Chris
0 Kudos
2 Replies
白李
by
New Contributor
String path = "/mnt/sdcard/stations.shp";
CrisPlutis
New Contributor

Thanks for the reply 白 李.

It was my fault because I forgot to explicitly request the READ_EXTERNAL_STORAGE permission in the application using ActivityCompat.requestPermissions() since the targetSdk was 23 (Marshmallow)  it wasn't enough to declare the permission in AndroidManifest.xml. In the end I just set 'targetSdkVersion 22' in build.gradle and everything worked fine.

Sorry for the trouble and thank you for your time.

0 Kudos