public static KmzParseResult parse(File albumFile) { final KmzParseResult result = new KmzParseResult(); result.isValid = false; final File kmlFile = new File(albumFile, KML_FILE_NAME); if (kmlFile == null || !kmlFile.exists()) { return result; } try { /** * You probably don't want IDs auto-generated when you're loading a document. You can always * turn it back on later. NOTE: KML updates will only work for objects that have an ID (either * autogenerated or assigned). */ Configuration.properties.setProperty(Configuration.GENERATE_IDS, Configuration.OFF); final Kml kml = KMLParser.parse(kmlFile); final String albumAbsPath = albumFile.getAbsolutePath(); final long albumId = Long.valueOf(albumFile.getName()); // Get folder Folder albumFolder = kml.getFolder(); if (albumFolder != null) { // Get name result.albumName = albumFolder.getName(); result.isValid = true; // Iterate placemarks result.pictures = new ArrayList(); Placemark[] placemarks = albumFolder.getPlacemarks(); for (int i = 0; i < placemarks.length; ++i) { Placemark placemark = placemarks[i]; // Get image path and text from description String[] imageAndText = toImageAndText(placemark.getDescription()); if (imageAndText[0] != null) { // Without an image, it is not interesting String pictureRelPath = imageAndText[0]; // Make sure the resource file is present if (new File(albumFile, pictureRelPath).exists()) { // Get point for coordinates Point point = placemark.getPoint(); if (point != null) { // Get coordinates double[] latLon = toLatLon(point.getCoordinates()); double latitude = latLon[0]; double longitude = latLon[1]; if (latitude != Constants.NO_GEO_TAG && longitude != Constants.NO_GEO_TAG) { // Without coordinates, it is not interesting // Get time long date = toTime(placemark.getTimeStamp()); // Get description String description = imageAndText[1]; if (description == null) { description = ""; } final String nameId = pictureRelPath.toLowerCase().replaceAll(Constants.JPG, ""); final Picture picture = new Picture(Long.valueOf(nameId), true, latitude, longitude, date); picture.setMiniPath(albumAbsPath + "/" + pictureRelPath); picture.setGroupId(albumId); picture.setDescription(description); result.pictures.add(picture); } } } } } } } catch (IOException e) { result.isValid = false; } catch (XmlPullParserException e) { result.isValid = false; } return result; }
/** * Create a placemark containing the information of the picture. If the picture file does not * already exist, copy it from the original picture, and update the picture's data members. * * @param albumFile * @param picture * @return the new placemark */ public static Placemark createPlacemark( File albumFile, Picture picture, ThumbManager thumbManager) { Placemark placemark = null; boolean fileExists = true; long imageId = picture.getId(); if (!new File(albumFile, imageId + Constants.JPG).exists()) { // The image does not already exist in the album, // so retrieve it, and copy it. fileExists = false; File srcFile = null; // 1. Try to retrieve it from its mini path. String miniPath = picture.getMiniPath(); if (miniPath != null) { final File thumbFile = new File(miniPath); if (thumbFile.exists()) { srcFile = thumbFile; fileExists = true; } } // 2. Try to retrieve it from the ThumbManager if (!fileExists) { Bitmap bitmap = thumbManager.getMiniBitmap(picture); if (bitmap != null) { miniPath = picture.getMiniPath(); final File thumbFile = new File(miniPath); if (thumbFile.exists()) { srcFile = thumbFile; fileExists = true; } bitmap.recycle(); bitmap = null; } } if (srcFile != null && fileExists) { imageId = FileUtils.generateId(); final File dstFile = new File(albumFile, imageId + Constants.JPG); try { FileUtils.copy(srcFile, dstFile); // Update picture instance with new values picture.setLocal(true); picture.setId(imageId); picture.setFullPath(null); picture.setMiniPath(dstFile.getAbsolutePath()); picture.setMicroPath(null); } catch (IOException e) { fileExists = false; } } } if (fileExists) { // Set group id on picture if (albumFile != null) { picture.setGroupId(Long.valueOf(albumFile.getName())); } // Create Placemark placemark = new Placemark(); final String path = String.valueOf(imageId) + Constants.JPG; placemark.setName(path); placemark.setDescription(toHTML(path, picture.getDescription())); placemark.addTimeStamp(toTimeStamp(picture.getDate())); // - Add Point final Point point = new Point(); point.setCoordinates(toCoordinates(picture.getLatitude(), picture.getLongitude())); placemark.addPoint(point); } return placemark; }