private boolean importGeoPackage ( String database , boolean override , InputStream geoPackageStream , GeoPackageProgress progress ) { try { if ( exists ( database ) ) { if ( override ) { if ( ! delete ( database ) ) { throw new GeoPackageException ( "Failed to delete existing database: " + database ) ; } } else { throw new GeoPackageException ( "GeoPackage database already exists: " + database ) ; } } // Copy the geopackage over as a database File newDbFile = context . getDatabasePath ( database ) ; try { SQLiteDatabase db = context . openOrCreateDatabase ( database , Context . MODE_PRIVATE , null ) ; db . close ( ) ; GeoPackageIOUtils . copyStream ( geoPackageStream , newDbFile , progress ) ; } catch ( IOException e ) { throw new GeoPackageException ( "Failed to import GeoPackage database: " + database , e ) ; } } finally { GeoPackageIOUtils . closeQuietly ( geoPackageStream ) ; } if ( progress == null || progress . isActive ( ) ) { // Verify that the database is valid try { SQLiteDatabase sqlite = context . openOrCreateDatabase ( database , Context . MODE_PRIVATE , null , new DatabaseErrorHandler ( ) { @ Override public void onCorruption ( SQLiteDatabase dbObj ) { } } ) ; validateDatabaseAndClose ( sqlite , importHeaderValidation , importIntegrityValidation ) ; GeoPackageMetadataDb metadataDb = new GeoPackageMetadataDb ( context ) ; metadataDb . open ( ) ; try { GeoPackageMetadataDataSource dataSource = new GeoPackageMetadataDataSource ( metadataDb ) ; // Save in metadata GeoPackageMetadata metadata = new GeoPackageMetadata ( ) ; metadata . setName ( database ) ; dataSource . create ( metadata ) ; } finally { metadataDb . close ( ) ; } } catch ( Exception e ) { delete ( database ) ; throw new GeoPackageException ( "Invalid GeoPackage database file" , e ) ; } GeoPackage geoPackage = open ( database , false ) ; if ( geoPackage != null ) { try { if ( ! geoPackage . getSpatialReferenceSystemDao ( ) . isTableExists ( ) || ! geoPackage . getContentsDao ( ) . isTableExists ( ) ) { delete ( database ) ; throw new GeoPackageException ( "Invalid GeoPackage database file. Does not contain required tables: " + SpatialReferenceSystem . TABLE_NAME + " & " + Contents . TABLE_NAME + ", Database: " + database ) ; } } catch ( SQLException e ) { delete ( database ) ; throw new GeoPackageException ( "Invalid GeoPackage database file. Could not verify existence of required tables: " + SpatialReferenceSystem . TABLE_NAME + " & " + Contents . TABLE_NAME + ", Database: " + database ) ; } finally { geoPackage . close ( ) ; } } else { delete ( database ) ; throw new GeoPackageException ( "Unable to open GeoPackage database. Database: " + database ) ; } } return exists ( database ) ; }
Import the GeoPackage stream