Example usage for android.net Uri getQuery

List of usage examples for android.net Uri getQuery

Introduction

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

Prototype

@Nullable
public abstract String getQuery();

Source Link

Document

Gets the decoded query component from this URI.

Usage

From source file:Main.java

public static Map<String, String> getValuesFromUri(@NonNull Uri uri) {
    return asMap(uri.getQuery() != null ? uri.getQuery() : uri.getFragment());
}

From source file:Main.java

public static String encodeQuery(String url) {
    Uri uri = Uri.parse(url);

    try {//  ww  w . j a v  a 2s.c o m
        String query = uri.getQuery();
        String encodedQuery = query != null ? URLEncoder.encode(query, "UTF-8") : null;
        URI tmp = new URI(uri.getScheme(), uri.getAuthority(), uri.getPath(), null, uri.getFragment());
        return tmp + (encodedQuery != null && encodedQuery.length() > 0 ? "?" + encodedQuery : "");
    } catch (UnsupportedEncodingException ignore) {
    } catch (URISyntaxException ignore) {
    }

    return uri.toString();
}

From source file:Main.java

/**
 * Splits the query parameters into key value pairs.
 * See: http://stackoverflow.com/a/13592567/534471.
 *//* w ww  .  j  a  v a  2  s . c  om*/
private static Map<String, List<String>> splitQuery(Uri uri) throws UnsupportedEncodingException {
    final Map<String, List<String>> query_pairs = new LinkedHashMap<>();
    String query = uri.getQuery();
    if (query == null)
        return query_pairs;

    final String[] pairs = query.split("&");
    for (String pair : pairs) {
        final int idx = pair.indexOf("=");
        final String key = idx > 0 ? URLDecoder.decode(pair.substring(0, idx), "UTF-8") : pair;
        if (!query_pairs.containsKey(key)) {
            query_pairs.put(key, new LinkedList<String>());
        }
        final String value = idx > 0 && pair.length() > idx + 1
                ? URLDecoder.decode(pair.substring(idx + 1), "UTF-8")
                : "";
        query_pairs.get(key).add(value);
    }
    return query_pairs;
}

From source file:Main.java

/**
 * Get a file path from a Uri./*from www  . j  ava2  s . co m*/
 * 
 * @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 w  w  .  j  a  v a  2  s .  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("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:Main.java

static Uri uriStripQueryParameter(Uri uri, String paramKey) {
    String queryParam = uri.getQueryParameter(paramKey);
    if (queryParam == null) {
        // nothing to strip
        return uri;
    } else {// w ww.j av  a2 s .c  o  m
        String uriString = uri.toString();
        String paramString = paramKey + "=" + queryParam;

        if (uri.getQuery().length() == paramString.length()) {
            paramString = "?" + paramString;
        } else if (uriString.length() - paramString.length() == uriString.indexOf(paramString)) {
            paramString = "&" + paramString;
        } else {
            paramString = paramString + "&";
        }
        return Uri.parse(uriString.replace(paramString, ""));
    }
}

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

/**
 * Get a file path from a Uri.// w w w  .  j  a  v a 2  s. co  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 www .j a  va2  s  .com
 *
 * 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:com.oakesville.mythling.util.MediaStreamProxy.java

public static URL getNetUrl(Uri mediaUri) throws MalformedURLException {
    String netUrl = mediaUri.getScheme() + "://" + mediaUri.getHost() + ":" + mediaUri.getPort();
    netUrl += mediaUri.getPath();//  w  w  w.  j  av a  2 s.  c o  m
    if (mediaUri.getQuery() != null)
        netUrl += "?" + mediaUri.getEncodedQuery();
    return new URL(netUrl);
}

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  av  a  2  s  . c om*/
 * 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;
}