Example usage for android.net Uri getPathSegments

List of usage examples for android.net Uri getPathSegments

Introduction

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

Prototype

public abstract List<String> getPathSegments();

Source Link

Document

Gets the decoded path segments.

Usage

From source file:Main.java

/**
 * Get a file path from a Uri./*from  w  w w  . j a  va 2 s .c  om*/
 * 
 * @param context
 * @param uri
 * @return
 * @throws URISyntaxException
 * 
 * @author paulburke
 */
public static String getPath(Context context, Uri uri) throws URISyntaxException {

    if (DEBUG)
        Log.d(TAG + " File -",
                "Authority: " + uri.getAuthority() + ", Fragment: " + uri.getFragment() + ", Port: "
                        + uri.getPort() + ", Query: " + uri.getQuery() + ", Scheme: " + uri.getScheme()
                        + ", Host: " + uri.getHost() + ", Segments: " + uri.getPathSegments().toString());

    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;

        try {
            cursor = context.getContentResolver().query(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow("_data");
            if (cursor.moveToFirst()) {
                return cursor.getString(column_index);
            }
        } catch (Exception e) {
            // Eat it
        }
    }

    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

From source file:id.nci.stm_9.FileHelper.java

/**
 * Get a file path from a Uri./*from   w  ww .  j ava  2 s  .  co m*/
 * 
 * from https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/
 * afilechooser/utils/FileUtils.java
 * 
 * @param context
 * @param uri
 * @return
 * 
 * @author paulburke
 */
public static String getPath(Context context, Uri uri) {
    Log.d("Stm-9" + " File -",
            "Authority: " + uri.getAuthority() + ", Fragment: " + uri.getFragment() + ", Port: " + uri.getPort()
                    + ", Query: " + uri.getQuery() + ", Scheme: " + uri.getScheme() + ", Host: " + uri.getHost()
                    + ", Segments: " + uri.getPathSegments().toString());

    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;

        try {
            cursor = context.getContentResolver().query(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow("_data");
            if (cursor.moveToFirst()) {
                return cursor.getString(column_index);
            }
        } catch (Exception e) {
            // Eat it
        }
    }

    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

From source file:com.rightscale.provider.Dashboard.java

static protected String _getType(Uri uri) {
    List<String> path = uri.getPathSegments();

    String model;//  w  w  w .jav  a  2s  .  com
    String mimePrefix, mimeType;

    if (path.size() % 2 == 1) {
        // Odd-sized paths (/deployments, /deployments/1/servers, ...)
        // represent a collection of resources.
        model = path.get(path.size() - 1);
        mimePrefix = "vnd.android.cursor.dir/";
    } else {
        // Even-sized paths (/deployments/1, /server_templates/1/executables/5)
        // represent an individual item.
        model = path.get(path.size() - 2);
        mimePrefix = "vnd.android.cursor.item/";
    }

    if (model.equals("accounts")) {
        mimeType = AccountsResource.MIME_TYPE;
    } else if (model.equals("deployments")) {
        mimeType = DeploymentsResource.MIME_TYPE;
    } else if (model.equals("servers")) {
        mimeType = ServersResource.MIME_TYPE;
    } else if (model.equals("server_settings")) {
        mimeType = ServerSettingsResource.MIME_TYPE;
    } else {
        throw new InvalidParameterException("Unknown URI: " + model);
    }

    return mimePrefix + mimeType;
}

From source file:org.sufficientlysecure.keychain.helper.FileHelper.java

/**
 * Get a file path from a Uri./*  www .j  a  v  a 2 s  .  c o  m*/
 * <p/>
 * from https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/
 * afilechooser/utils/FileUtils.java
 *
 * @param context
 * @param uri
 * @return
 * @author paulburke
 */
public static String getPath(Context context, Uri uri) {
    Log.d(Constants.TAG + " File -",
            "Authority: " + uri.getAuthority() + ", Fragment: " + uri.getFragment() + ", Port: " + uri.getPort()
                    + ", Query: " + uri.getQuery() + ", Scheme: " + uri.getScheme() + ", Host: " + uri.getHost()
                    + ", Segments: " + uri.getPathSegments().toString());

    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
        try {
            if (cursor != null && cursor.moveToFirst()) {
                int columnIndex = cursor.getColumnIndexOrThrow("_data");
                return cursor.getString(columnIndex);
            }
        } catch (Exception e) {
            // Eat it
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

From source file:org.thialfihar.android.apg.helper.FileHelper.java

/**
 * Get a file path from a Uri.//from   w  ww.j ava2s . c om
 *
 * from https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/
 * afilechooser/utils/FileUtils.java
 *
 * @param context
 * @param uri
 * @return
 *
 * @author paulburke
 */
public static String getPath(Context context, Uri uri) {
    Log.d(Constants.TAG + " File -",
            "Authority: " + uri.getAuthority() + ", Fragment: " + uri.getFragment() + ", Port: " + uri.getPort()
                    + ", Query: " + uri.getQuery() + ", Scheme: " + uri.getScheme() + ", Host: " + uri.getHost()
                    + ", Segments: " + uri.getPathSegments().toString());

    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;

        try {
            cursor = context.getContentResolver().query(uri, projection, null, null, null);
            int columnIndex = cursor.getColumnIndexOrThrow("_data");
            if (cursor.moveToFirst()) {
                return cursor.getString(columnIndex);
            }
        } catch (Exception e) {
            // Eat it
        }
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

From source file:edu.mit.mobile.android.locast.data.TaggableItem.java

/**
 * Given a base content URI of a taggable item and a list of tags, constructs a URI
 * representing all the items of the baseUri that match all the listed tags.
 *
 * @param baseUri a content URI of a TaggableItem
 * @param tags a collection of tags//from  ww w. j a va  2  s.  c  o m
 * @return a URI representing all the items that match all the given tags
 */
public static Uri getTagUri(Uri baseUri, Collection<String> tags) {
    if (tags.isEmpty()) {
        return baseUri;
    }

    final List<String> path = baseUri.getPathSegments();
    // AUTHORITY/casts/tags
    if (path.size() >= 2 && Tag.PATH.equals(path.get(path.size() - 1))) {
        baseUri = ProviderUtils.removeLastPathSegment(baseUri);
    }

    return baseUri.buildUpon().appendQueryParameter(SERVER_QUERY_PARAMETER, Tag.toTagQuery(tags)).build();
}

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.<br>
 * <br>/*  w  w w . j a v  a2s.  c  o m*/
 * Callers should check whether the path is local before assuming it
 * represents a local file.
 *
 * @param context The context.
 * @param uri     The Uri to query.
 * @author paulburke
 * @see #isLocal(String)
 * @see #getFile(Context, Uri)
 */
@SuppressLint("NewApi")
public static String getPath(final Context context, final Uri uri) {

    if (DEBUG)
        Log.d(TAG + " File -",
                "Authority: " + uri.getAuthority() + ", Fragment: " + uri.getFragment() + ", Port: "
                        + uri.getPort() + ", Query: " + uri.getQuery() + ", Scheme: " + uri.getScheme()
                        + ", Host: " + uri.getHost() + ", Segments: " + uri.getPathSegments().toString());

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

    // DocumentProvider
    if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
        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];
            }

            // 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:com.rightscale.provider.Dashboard.java

static public void performAction(Context context, Uri uri, String accountId, String action, String param)
        throws DashboardError {
    String mimeType = _getType(uri);

    if (!mimeType.startsWith("vnd.android.cursor.item/")) {
        throw new DashboardError("Cannot perform actions on collection resource " + uri.toString());
    }// w  w w .  j  a v a2 s. co  m

    List<String> pathSegments = uri.getPathSegments();
    String id = pathSegments.get(pathSegments.size() - 1);

    try {
        DashboardSession session = createSession(context);
        session.login();

        if (mimeType.endsWith(ServersResource.MIME_TYPE)) {
            ServersResource servers = new ServersResource(session, accountId);

            if (ACTION_LAUNCH.equals(action)) {
                servers.launch(id);

            } else if (ACTION_TERMINATE.equals(action)) {
                servers.terminate(id);

            } else if (ACTION_REBOOT.equals(action)) {
                servers.reboot(id);
            } else if (ACTION_RUN_SCRIPT.equals(action)) {
                servers.runScript(id, (String) param);
            }
        } else {
            throw new DashboardError("Cannot perform actions on unknown URI " + uri.toString());
        }
    } catch (RestException e) {
        forgetSession();
        Error err = new DashboardError(e);
        throw err;
    }
}

From source file:com.frostwire.android.LollipopFileSystem.java

private static String getTreeDocumentId(Uri documentUri) {
    final List<String> paths = documentUri.getPathSegments();
    if (paths.size() >= 2 && "tree".equals(paths.get(0))) {
        return paths.get(1);
    }//from w  w  w  .  j a  v a  2  s.  co m
    throw new IllegalArgumentException("Invalid URI: " + documentUri);
}

From source file:com.frostwire.android.LollipopFileSystem.java

private static String getDocumentDocumentId(Uri documentUri) {
    final List<String> paths = documentUri.getPathSegments();
    if (paths.size() >= 4 && "document".equals(paths.get(2))) {
        return paths.get(3);
    }//from  www  . ja  va 2  s. c  om
    throw new IllegalArgumentException("Invalid URI: " + documentUri);
}