private void serializeWaypoints(Context context, XmlSerializer serializer, Uri media) throws IOException { if (isCancelled()) { throw new IOException("Fail to execute request due to canceling"); } Cursor mediaCursor = null; Cursor waypointCursor = null; BufferedReader buf = null; ContentResolver resolver = context.getContentResolver(); try { mediaCursor = resolver.query( media, new String[] {Media.URI, Media.TRACK, Media.SEGMENT, Media.WAYPOINT}, null, null, null); if (mediaCursor.moveToFirst()) { do { Uri waypointUri = Waypoints.buildUri( mediaCursor.getLong(1), mediaCursor.getLong(2), mediaCursor.getLong(3)); waypointCursor = resolver.query( waypointUri, new String[] { Waypoints.LATITUDE, Waypoints.LONGITUDE, Waypoints.ALTITUDE, Waypoints.TIME }, null, null, null); serializer.text("\n"); serializer.startTag("", "wpt"); if (waypointCursor != null && waypointCursor.moveToFirst()) { serializer.attribute(null, "lat", Double.toString(waypointCursor.getDouble(0))); serializer.attribute(null, "lon", Double.toString(waypointCursor.getDouble(1))); serializer.text("\n"); serializer.startTag("", "ele"); serializer.text(Double.toString(waypointCursor.getDouble(2))); serializer.endTag("", "ele"); serializer.text("\n"); serializer.startTag("", "time"); Date time = new Date(waypointCursor.getLong(3)); synchronized (ZULU_DATE_FORMATER) { serializer.text(ZULU_DATE_FORMATER.format(time)); } serializer.endTag("", "time"); } if (waypointCursor != null) { waypointCursor.close(); waypointCursor = null; } Uri mediaUri = Uri.parse(mediaCursor.getString(0)); if (mediaUri.getScheme().equals("file")) { if (mediaUri.getLastPathSegment().endsWith("3gp")) { String fileName = includeMediaFile(mediaUri.getLastPathSegment()); quickTag(serializer, "", "name", fileName); serializer.startTag("", "link"); serializer.attribute(null, "href", fileName); quickTag(serializer, "", "text", fileName); serializer.endTag("", "link"); } else if (mediaUri.getLastPathSegment().endsWith("jpg")) { String mediaPathPrefix = Constants.getSdCardDirectory(mContext); String fileName = includeMediaFile(mediaPathPrefix + mediaUri.getLastPathSegment()); quickTag(serializer, "", "name", fileName); serializer.startTag("", "link"); serializer.attribute(null, "href", fileName); quickTag(serializer, "", "text", fileName); serializer.endTag("", "link"); } else if (mediaUri.getLastPathSegment().endsWith("txt")) { quickTag(serializer, "", "name", mediaUri.getLastPathSegment()); serializer.startTag("", "desc"); if (buf != null) { buf.close(); } buf = new BufferedReader(new FileReader(mediaUri.getEncodedPath())); String line; while ((line = buf.readLine()) != null) { serializer.text(line); serializer.text("\n"); } serializer.endTag("", "desc"); } } else if (mediaUri.getScheme().equals("content")) { if ((GPStracking.AUTHORITY + ".string").equals(mediaUri.getAuthority())) { quickTag(serializer, "", "name", mediaUri.getLastPathSegment()); } else if (mediaUri.getAuthority().equals("media")) { Cursor mediaItemCursor = null; try { mediaItemCursor = resolver.query( mediaUri, new String[] {MediaColumns.DATA, MediaColumns.DISPLAY_NAME}, null, null, null); if (mediaItemCursor.moveToFirst()) { String fileName = includeMediaFile(mediaItemCursor.getString(0)); quickTag(serializer, "", "name", fileName); serializer.startTag("", "link"); serializer.attribute(null, "href", fileName); quickTag(serializer, "", "text", mediaItemCursor.getString(1)); serializer.endTag("", "link"); } } finally { if (mediaItemCursor != null) { mediaItemCursor.close(); } } } } serializer.text("\n"); serializer.endTag("", "wpt"); } while (mediaCursor.moveToNext()); } } finally { if (mediaCursor != null) { mediaCursor.close(); } if (waypointCursor != null) { waypointCursor.close(); } if (buf != null) buf.close(); } }
protected Uri exportGpx() { String xmlFilePath; if (mFileName.endsWith(".gpx") || mFileName.endsWith(".xml")) { setExportDirectoryPath( Constants.getSdCardDirectory(mContext) + mFileName.substring(0, mFileName.length() - 4)); xmlFilePath = getExportDirectoryPath() + "/" + mFileName; } else { setExportDirectoryPath(Constants.getSdCardDirectory(mContext) + mFileName); xmlFilePath = getExportDirectoryPath() + "/" + mFileName + ".gpx"; } new File(getExportDirectoryPath()).mkdirs(); String resultFilename = null; FileOutputStream fos = null; BufferedOutputStream buf = null; try { verifySdCardAvailibility(); XmlSerializer serializer = Xml.newSerializer(); File xmlFile = new File(xmlFilePath); fos = new FileOutputStream(xmlFile); buf = new BufferedOutputStream(fos, 8 * 8192); serializer.setOutput(buf, "UTF-8"); serializeTrack(mTrackUri, serializer); buf.close(); buf = null; fos.close(); fos = null; if (needsBundling()) { resultFilename = bundlingMediaAndXml(xmlFile.getParentFile().getName(), ".zip"); } else { File finalFile = new File(Constants.getSdCardDirectory(mContext) + xmlFile.getName()); xmlFile.renameTo(finalFile); resultFilename = finalFile.getAbsolutePath(); XmlCreator.deleteRecursive(xmlFile.getParentFile()); } mFileName = new File(resultFilename).getName(); } catch (FileNotFoundException e) { String text = mContext.getString(R.string.ticker_failed) + " \"" + xmlFilePath + "\" " + mContext.getString(R.string.error_filenotfound); handleError(mContext.getString(R.string.taskerror_gpx_write), e, text); } catch (IllegalArgumentException e) { String text = mContext.getString(R.string.ticker_failed) + " \"" + xmlFilePath + "\" " + mContext.getString(R.string.error_filename); handleError(mContext.getString(R.string.taskerror_gpx_write), e, text); } catch (IllegalStateException e) { String text = mContext.getString(R.string.ticker_failed) + " \"" + xmlFilePath + "\" " + mContext.getString(R.string.error_buildxml); handleError(mContext.getString(R.string.taskerror_gpx_write), e, text); } catch (IOException e) { String text = mContext.getString(R.string.ticker_failed) + " \"" + xmlFilePath + "\" " + mContext.getString(R.string.error_writesdcard); handleError(mContext.getString(R.string.taskerror_gpx_write), e, text); } finally { if (buf != null) { try { buf.close(); } catch (IOException e) { Log.e(TAG, "Failed to close buf after completion, ignoring.", e); } } if (fos != null) { try { fos.close(); } catch (IOException e) { Log.e(TAG, "Failed to close fos after completion, ignoring.", e); } } } return Uri.fromFile(new File(resultFilename)); }