Example usage for android.net Uri getLastPathSegment

List of usage examples for android.net Uri getLastPathSegment

Introduction

In this page you can find the example usage for android.net Uri getLastPathSegment.

Prototype

@Nullable
public abstract String getLastPathSegment();

Source Link

Document

Gets the decoded last segment in the path.

Usage

From source file:com.nogago.android.tracks.services.TrackRecordingService.java

public long insertWaypoint(WaypointCreationRequest request) {
    if (!isRecording()) {
        throw new IllegalStateException("Unable to insert marker while not recording!");
    }//from w  w  w . ja  va2s  .c o m
    Waypoint waypoint = new Waypoint();
    if (request.getType() == WaypointType.WAYPOINT) {
        buildWaypointMarker(waypoint, request);
    } else {
        buildStatisticsMarker(waypoint, request);
    }
    waypoint.setTrackId(recordingTrackId);
    waypoint.setLength(length);
    if (lastLocation == null || statsBuilder == null || statsBuilder.getStatistics() == null) {
        if (!request.isTrackStatistics()) {
            return -1L;
        }
        /*
         * For track statistics, a null location is OK. Make it an impossible
         * location.
         */
        Location location = new Location("");
        location.setLatitude(100);
        location.setLongitude(180);
        waypoint.setLocation(location);
    } else {
        waypoint.setLocation(lastLocation);
        waypoint.setDuration(lastLocation.getTime() - statsBuilder.getStatistics().getStartTime());
    }
    Uri uri = providerUtils.insertWaypoint(waypoint);
    return Long.parseLong(uri.getLastPathSegment());
}

From source file:ch.ethz.twimight.net.opportunistic.ScanningService.java

/**
 * Creates a JSON Object from a Tweet TODO: Move this where it belongs!
 * /*from  w ww  .  j ava 2 s . c  om*/
 * @param c
 * @return
 * @throws JSONException
 */
protected JSONObject getJSON(Cursor c) throws JSONException {
    JSONObject o = new JSONObject();
    if (c.getColumnIndex(Tweets.COL_USER_TID) < 0 || c.getColumnIndex(TwitterUsers.COL_SCREEN_NAME) < 0) {
        Log.i(TAG, "missing user data");
        return null;
    }

    else {

        o.put(Tweets.COL_USER_TID, c.getLong(c.getColumnIndex(Tweets.COL_USER_TID)));
        o.put(TYPE, MESSAGE_TYPE_TWEET);
        o.put(TwitterUsers.COL_SCREEN_NAME, c.getString(c.getColumnIndex(TwitterUsers.COL_SCREEN_NAME)));
        if (c.getColumnIndex(Tweets.COL_CREATED_AT) >= 0)
            o.put(Tweets.COL_CREATED_AT, c.getLong(c.getColumnIndex(Tweets.COL_CREATED_AT)));
        if (c.getColumnIndex(Tweets.COL_CERTIFICATE) >= 0)
            o.put(Tweets.COL_CERTIFICATE, c.getString(c.getColumnIndex(Tweets.COL_CERTIFICATE)));
        if (c.getColumnIndex(Tweets.COL_SIGNATURE) >= 0)
            o.put(Tweets.COL_SIGNATURE, c.getString(c.getColumnIndex(Tweets.COL_SIGNATURE)));

        if (c.getColumnIndex(Tweets.COL_TEXT) >= 0)
            o.put(Tweets.COL_TEXT, c.getString(c.getColumnIndex(Tweets.COL_TEXT)));
        if (c.getColumnIndex(Tweets.COL_REPLY_TO_TWEET_TID) >= 0)
            o.put(Tweets.COL_REPLY_TO_TWEET_TID, c.getLong(c.getColumnIndex(Tweets.COL_REPLY_TO_TWEET_TID)));
        if (c.getColumnIndex(Tweets.COL_LAT) >= 0)
            o.put(Tweets.COL_LAT, c.getDouble(c.getColumnIndex(Tweets.COL_LAT)));
        if (c.getColumnIndex(Tweets.COL_LNG) >= 0)
            o.put(Tweets.COL_LNG, c.getDouble(c.getColumnIndex(Tweets.COL_LNG)));
        if (!c.isNull(c.getColumnIndex(Tweets.COL_MEDIA_URIS))) {
            String photoUri = c.getString(c.getColumnIndex(Tweets.COL_MEDIA_URIS));
            Uri uri = Uri.parse(photoUri);
            o.put(Tweets.COL_MEDIA_URIS, uri.getLastPathSegment());
        }
        if (c.getColumnIndex(Tweets.COL_HTML_PAGES) >= 0)
            o.put(Tweets.COL_HTML_PAGES, c.getString(c.getColumnIndex(Tweets.COL_HTML_PAGES)));
        if (c.getColumnIndex(Tweets.COL_SOURCE) >= 0)
            o.put(Tweets.COL_SOURCE, c.getString(c.getColumnIndex(Tweets.COL_SOURCE)));

        if (c.getColumnIndex(Tweets.COL_TID) >= 0 && !c.isNull(c.getColumnIndex(Tweets.COL_TID)))
            o.put(Tweets.COL_TID, c.getLong(c.getColumnIndex(Tweets.COL_TID)));

        if (c.getColumnIndex(TwitterUsers.COL_PROFILE_IMAGE_URI) >= 0
                && c.getColumnIndex(TweetsContentProvider.COL_USER_ROW_ID) >= 0) {

            String imageUri = c.getString(c.getColumnIndex(TwitterUsers.COL_PROFILE_IMAGE_URI));
            Bitmap profileImage = ImageLoader.getInstance().loadImageSync(imageUri);
            if (profileImage != null) {
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                profileImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
                byte[] byteArray = stream.toByteArray();
                String profileImageBase64 = Base64.encodeToString(byteArray, Base64.DEFAULT);
                o.put(TwitterUsers.JSON_FIELD_PROFILE_IMAGE, profileImageBase64);
            }
        }

        return o;
    }
}

From source file:org.cowboycoders.cyclisimo.services.TrackRecordingService.java

/**
 * Starts a new track.//www  .j ava2 s  .c  o  m
 * 
 * @return the track id
 */
private long startNewTrack() {
    if (isRecording()) {
        Log.d(TAG, "Ignore startNewTrack. Already recording.");
        return -1L;
    }
    long now = System.currentTimeMillis();
    trackTripStatisticsUpdater = new TripStatisticsUpdater(now);
    markerTripStatisticsUpdater = new TripStatisticsUpdater(now);

    long currentUserId = PreferencesUtils.getLong(this, R.string.settings_select_user_current_selection_key);

    // Insert a track
    Track track = new Track();
    track.setOwner(currentUserId);
    Uri uri = myTracksProviderUtils.insertTrack(track);
    long trackId = Long.parseLong(uri.getLastPathSegment());

    // Update shared preferences
    updateRecordingState(trackId, false);
    PreferencesUtils.setInt(this, R.string.auto_resume_track_current_retry_key, 0);

    // Update database
    track.setId(trackId);
    track.setName(TrackNameUtils.getTrackName(this, trackId, now, null));
    track.setCategory(PreferencesUtils.getString(this, R.string.default_activity_key,
            PreferencesUtils.DEFAULT_ACTIVITY_DEFAULT));
    track.setTripStatistics(trackTripStatisticsUpdater.getTripStatistics());
    myTracksProviderUtils.updateTrack(track);
    insertWaypoint(WaypointCreationRequest.DEFAULT_START_TRACK);

    startRecording(true);
    return trackId;
}

From source file:Main.java

/**
 * Get a file path from a Uri. This will get the the path for Storage Access
 * Framework Documents, as well as the _data field for the MediaStore and
 * other file-based ContentProviders.//from ww  w.j  ava  2 s  .  co m
 *
 * @param context The context.
 * @param uri     The Uri to query.
 */
@SuppressLint("NewApi")
public static String getPath(final Context context, final Uri uri) {

    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

    // DocumentProvider
    if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
        // ExternalStorageProvider
        if (isExternalStorageDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            if ("primary".equalsIgnoreCase(type)) {
                return Environment.getExternalStorageDirectory() + "/" + split[1];
            }

        } else if (isDownloadsDocument(uri)) {
            // DownloadsProvider
            final String id = DocumentsContract.getDocumentId(uri);
            final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
                    Long.valueOf(id));

            return getDataColumn(context, contentUri, null, null);
        } else if (isMediaDocument(uri)) {
            // MediaProvider
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            Uri contentUri = null;
            if ("image".equals(type)) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }

            final String selection = "_id=?";
            final String[] selectionArgs = new String[] { split[1] };

            return getDataColumn(context, contentUri, selection, selectionArgs);
        }
    } else if ("content".equalsIgnoreCase(uri.getScheme())) {
        // MediaStore (and general)
        // Return the remote address
        if (isGooglePhotosUri(uri)) {
            return uri.getLastPathSegment();
        }

        return getDataColumn(context, uri, null, null);
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        // File
        return uri.getPath();
    }

    return null;
}

From source file:Main.java

@SuppressLint("NewApi")
public static String getPath(final Context context, final Uri uri) {
    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
    // DocumentProvider
    if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
        // ExternalStorageProvider
        if (isExternalStorageDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            if ("primary".equalsIgnoreCase(type)) {
                return Environment.getExternalStorageDirectory() + "/" + split[1];
            }//from w w  w  .j a v a2  s .co m
        }
        // DownloadsProvider
        else if (isDownloadsDocument(uri)) {
            final String id = DocumentsContract.getDocumentId(uri);
            final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
                    Long.valueOf(id));
            return getDataColumn(context, contentUri, null, null);
        }
        // MediaProvider
        else if (isMediaDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];
            Uri contentUri = null;
            if ("image".equals(type)) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }

            final String selection = "_id=?";
            final String[] selectionArgs = new String[] { split[1] };

            return getDataColumn(context, contentUri, selection, selectionArgs);
        }
    }
    // MediaStore (and general)
    else if ("content".equalsIgnoreCase(uri.getScheme())) {
        // Return the remote address
        if (isGooglePhotosUri(uri))
            return uri.getLastPathSegment();
        return getDataColumn(context, uri, null, null);
    }
    // File
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }
    return null;
}

From source file:com.nogago.android.tracks.services.TrackRecordingService.java

private long startNewTrack() {
    Log.d(TAG, "TrackRecordingService.startNewTrack");
    if (isTrackInProgress()) {
        return -1L;
    }/*w ww.j  ava 2 s  .c  o m*/

    long startTime = System.currentTimeMillis();
    acquireWakeLock();

    Track track = new Track();
    TripStatistics trackStats = track.getTripStatistics();
    trackStats.setStartTime(startTime);
    track.setStartId(-1);
    Uri trackUri = providerUtils.insertTrack(track);
    recordingTrackId = Long.parseLong(trackUri.getLastPathSegment());
    track.setId(recordingTrackId);
    track.setName(TrackNameUtils.getTrackName(this, recordingTrackId, startTime, null));
    track.setCategory(PreferencesUtils.getString(this, R.string.default_activity_key,
            PreferencesUtils.DEFAULT_ACTIVITY_DEFAULT));
    isRecording = true;
    isMoving = true;

    providerUtils.updateTrack(track);
    statsBuilder = new TripStatisticsBuilder(startTime);
    statsBuilder.setMinRecordingDistance(minRecordingDistance);
    waypointStatsBuilder = new TripStatisticsBuilder(startTime);
    waypointStatsBuilder.setMinRecordingDistance(minRecordingDistance);
    currentWaypointId = insertWaypoint(WaypointCreationRequest.DEFAULT_START_TRACK);
    length = 0;
    showNotification();
    registerLocationListener();
    sensorManager = SensorManagerFactory.getSystemSensorManager(this);

    // Reset the number of auto-resume retries.
    setAutoResumeTrackRetries(0);
    // Persist the current recording track.
    PreferencesUtils.setLong(this, R.string.recording_track_id_key, recordingTrackId);

    // Notify the world that we're now recording.
    sendTrackBroadcast(R.string.track_started_broadcast_action, recordingTrackId);
    announcementExecutor.restore();
    splitExecutor.restore();

    return recordingTrackId;
}

From source file:Main.java

@TargetApi(Build.VERSION_CODES.KITKAT)
public static String getPath(final Context context, final Uri uri) {

    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

    // DocumentProvider
    if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
        // ExternalStorageProvider
        if (isExternalStorageDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            if ("primary".equalsIgnoreCase(type)) {
                return Environment.getExternalStorageDirectory() + "/" + split[1];
            }/*ww w.j a v a 2s.co m*/
        }
        // DownloadsProvider
        else if (isDownloadsDocument(uri)) {

            final String id = DocumentsContract.getDocumentId(uri);
            final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
                    Long.valueOf(id));

            return getDataColumn(context, contentUri, null, null);
        }
        // MediaProvider
        else if (isMediaDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            Uri contentUri = null;
            if ("image".equals(type)) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }

            final String selection = "_id=?";
            final String[] selectionArgs = new String[] { split[1] };

            return getDataColumn(context, contentUri, selection, selectionArgs);
        }
    }
    // MediaStore (and general)
    else if ("content".equalsIgnoreCase(uri.getScheme())) {

        // Return the remote address
        if (isGooglePhotosUri(uri))
            return uri.getLastPathSegment();

        return getDataColumn(context, uri, null, null);
    }
    // File
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }
    return "";
}

From source file:Main.java

@SuppressLint("NewApi")
public static String getPath(final Context context, final Uri uri) {
    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
    // DocumentProvider
    if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
        // ExternalStorageProvider
        if (isExternalStorageDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];
            if ("primary".equalsIgnoreCase(type)) {
                return Environment.getExternalStorageDirectory() + "/" + split[1];
            }/*from w  ww  .j a v  a 2s.c  o  m*/
            // TODO handle non-primary volumes
        }
        // DownloadsProvider
        else if (isDownloadsDocument(uri)) {
            final String id = DocumentsContract.getDocumentId(uri);
            final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
                    Long.valueOf(id));
            return getDataColumn(context, contentUri, null, null);
        }
        // MediaProvider
        else if (isMediaDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];
            Uri contentUri = null;
            if ("image".equals(type)) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }
            final String selection = "_id=?";
            final String[] selectionArgs = new String[] { split[1] };
            return getDataColumn(context, contentUri, selection, selectionArgs);
        }
    }
    // MediaStore (and general)
    else if ("content".equalsIgnoreCase(uri.getScheme())) {
        // Return the remote address
        if (isGooglePhotosUri(uri))
            return uri.getLastPathSegment();
        return getDataColumn(context, uri, null, null);
    }
    // File
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }
    return null;
}

From source file:Main.java

public static String getRealPath(final Context context, final Uri uri) {

    // DocumentProvider
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && DocumentsContract.isDocumentUri(context, uri)) {
        // ExternalStorageProvider
        if (isExternalStorageDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            if ("primary".equalsIgnoreCase(type)) {
                return Environment.getExternalStorageDirectory() + "/" + split[1];
            }/*from  w  ww .j  a  va 2  s.  co  m*/

            // TODO handle non-primary volumes
        }
        // DownloadsProvider
        else if (isDownloadsDocument(uri)) {

            final String id = DocumentsContract.getDocumentId(uri);
            final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
                    Long.valueOf(id));

            return getDataColumn(context, contentUri, null, null);
        }
        // MediaProvider
        else if (isMediaDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            Uri contentUri = null;
            if ("image".equals(type)) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }

            final String selection = "_id=?";
            final String[] selectionArgs = new String[] { split[1] };

            return getDataColumn(context, contentUri, selection, selectionArgs);
        }
    }
    // MediaStore (and general)
    else if ("content".equalsIgnoreCase(uri.getScheme())) {

        // Return the remote address
        if (isGooglePhotosUri(uri))
            return uri.getLastPathSegment();

        return getDataColumn(context, uri, null, null);
    }
    // File
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

From source file:org.cowboycoders.cyclisimo.services.TrackRecordingService.java

/**
 * Inserts a waypoint./*from   ww w  .ja  v a 2 s. c om*/
 * 
 * @param waypointCreationRequest the waypoint creation request
 * @return the waypoint id
 */
public long insertWaypoint(WaypointCreationRequest waypointCreationRequest) {
    if (!isRecording() || isPaused()) {
        return -1L;
    }

    boolean isStatistics = waypointCreationRequest.getType() == WaypointType.STATISTICS;
    String name;
    if (waypointCreationRequest.getName() != null) {
        name = waypointCreationRequest.getName();
    } else {
        int nextWaypointNumber = myTracksProviderUtils.getNextWaypointNumber(recordingTrackId, isStatistics);
        if (nextWaypointNumber == -1) {
            nextWaypointNumber = 0;
        }
        name = getString(isStatistics ? R.string.marker_split_name_format : R.string.marker_name_format,
                nextWaypointNumber);
    }

    TripStatistics tripStatistics;
    String description;
    if (isStatistics) {
        long now = System.currentTimeMillis();
        markerTripStatisticsUpdater.updateTime(now);
        tripStatistics = markerTripStatisticsUpdater.getTripStatistics();
        markerTripStatisticsUpdater = new TripStatisticsUpdater(now);
        description = new DescriptionGeneratorImpl(this).generateWaypointDescription(tripStatistics);
    } else {
        tripStatistics = null;
        description = waypointCreationRequest.getDescription() != null
                ? waypointCreationRequest.getDescription()
                : "";
    }

    String category = waypointCreationRequest.getCategory() != null ? waypointCreationRequest.getCategory()
            : "";
    String icon = getString(
            isStatistics ? R.string.marker_statistics_icon_url : R.string.marker_waypoint_icon_url);
    int type = isStatistics ? Waypoint.TYPE_STATISTICS : Waypoint.TYPE_WAYPOINT;
    long duration;
    double length;
    Location location = getLastValidTrackPointInCurrentSegment(recordingTrackId);
    if (location != null && trackTripStatisticsUpdater != null) {
        TripStatistics stats = trackTripStatisticsUpdater.getTripStatistics();
        length = stats.getTotalDistance();
        duration = stats.getTotalTime();
    } else {
        if (!waypointCreationRequest.isTrackStatistics()) {
            return -1L;
        }
        // For track statistics, make it an impossible location
        location = new Location("");
        location.setLatitude(100);
        location.setLongitude(180);
        length = 0;
        duration = 0;
    }
    Waypoint waypoint = new Waypoint(name, description, category, icon, recordingTrackId, type, length,
            duration, -1L, -1L, location, tripStatistics);
    Uri uri = myTracksProviderUtils.insertWaypoint(waypoint);
    return Long.parseLong(uri.getLastPathSegment());
}