Example usage for android.net Uri getScheme

List of usage examples for android.net Uri getScheme

Introduction

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

Prototype

@Nullable
public abstract String getScheme();

Source Link

Document

Gets the scheme of this URI.

Usage

From source file:com.portol.common.utils.Util.java

/**
 * Merges a uri and a string to produce a new uri.
 * <p>//www  . jav a  2 s. c o m
 * The uri is built according to the following rules:
 * <ul>
 * <li>If {@code baseUri} is null or if {@code stringUri} is absolute, then {@code baseUri} is
 * ignored and the uri consists solely of {@code stringUri}.
 * <li>If {@code stringUri} is null, then the uri consists solely of {@code baseUrl}.
 * <li>Otherwise, the uri consists of the concatenation of {@code baseUri} and {@code stringUri}.
 * </ul>
 *
 * @param baseUri A uri that can form the base of the merged uri.
 * @param stringUri A relative or absolute uri in string form.
 * @return The merged uri.
 */
public static Uri getMergedUri(Uri baseUri, String stringUri) {
    if (stringUri == null) {
        return baseUri;
    }
    if (baseUri == null) {
        return Uri.parse(stringUri);
    }
    if (stringUri.startsWith("/")) {
        stringUri = stringUri.substring(1);
        return new Uri.Builder().scheme(baseUri.getScheme()).authority(baseUri.getAuthority())
                .appendEncodedPath(stringUri).build();
    }
    Uri uri = Uri.parse(stringUri);
    if (uri.isAbsolute()) {
        return uri;
    }
    return Uri.withAppendedPath(baseUri, stringUri);
}

From source file:com.jefftharris.passwdsafe.file.PasswdFileUri.java

/** Get the URI type */
private static Type getUriType(Uri uri) {
    if (uri.getScheme().equals(ContentResolver.SCHEME_FILE)) {
        return Type.FILE;
    }/* ww  w . j ava2  s .  co m*/
    String auth = uri.getAuthority();
    if (PasswdSafeContract.AUTHORITY.equals(auth)) {
        return Type.SYNC_PROVIDER;
    } else if (auth.contains("mail")) {
        return Type.EMAIL;
    }
    return Type.GENERIC_PROVIDER;
}

From source file:com.appunite.websocket.WebSocket.java

/**
 * Return if given uri is ssl encrypted (thread safe)
 * //from w w w. jav a2 s  . c om
 * @param uri uri to check
 * @return true if uri is wss
 * @throws IllegalArgumentException
 *             if unkonwo schema
 */
private static boolean isSsl(Uri uri) {
    String scheme = uri.getScheme();
    if ("wss".equals(scheme)) {
        return true;
    } else if ("ws".equals(scheme)) {
        return false;
    } else {
        throw new IllegalArgumentException("Unknown schema");
    }
}

From source file:com.ultramegasoft.flavordex2.util.PhotoUtils.java

/**
 * Get the simplest representation of a photo path from a Uri.
 *
 * @param uri The photo Uri/*from  w w  w.ja  v  a  2s.c  om*/
 * @return The file name or full Uri string
 */
@NonNull
static String getPathString(@NonNull Uri uri) {
    if ("file".equals(uri.getScheme())) {
        final File file = new File(uri.getPath());
        try {
            if (file.getParentFile().equals(getMediaStorageDir())) {
                return file.getName();
            }
        } catch (IOException e) {
            Log.w(TAG, "Unable to check file location", e);
        }
    }
    return uri.toString();
}

From source file:Main.java

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

    try {//from www  .  j a v a  2  s.  co m
        Map<String, List<String>> splitQuery = splitQuery(uri);
        StringBuilder encodedQuery = new StringBuilder();
        for (String key : splitQuery.keySet()) {
            for (String value : splitQuery.get(key)) {
                if (encodedQuery.length() > 0) {
                    encodedQuery.append("&");
                }
                encodedQuery.append(key + "=" + URLEncoder.encode(value, "UTF-8"));
            }
        }
        String queryString = encodedQuery != null && encodedQuery.length() > 0 ? "?" + encodedQuery : "";

        URI baseUri = new URI(uri.getScheme(), uri.getAuthority(), uri.getPath(), null, uri.getFragment());
        return baseUri + queryString;
    } catch (UnsupportedEncodingException ignore) {
    } catch (URISyntaxException ignore) {
    }

    return uri.toString();
}

From source file:com.ultramegasoft.flavordex2.util.PhotoUtils.java

/**
 * Get the file name from a content Uri.
 *
 * @param cr  The ContentResolver//from  w  w w. j a v a  2  s .  c  o m
 * @param uri The Uri
 * @return The file name
 */
@Nullable
private static String getName(@NonNull ContentResolver cr, @NonNull Uri uri) {
    if ("file".equals(uri.getScheme())) {
        return uri.getLastPathSegment();
    } else {
        final String[] projection = new String[] { MediaStore.MediaColumns.DISPLAY_NAME };
        final Cursor cursor = cr.query(uri, projection, null, null, null);
        if (cursor != null) {
            try {
                if (cursor.moveToFirst()) {
                    return cursor.getString(0);
                }
            } finally {
                cursor.close();
            }
        }
    }
    return null;
}

From source file:org.apache.cordova.engine.crosswalk.XWalkCordovaWebViewClient.java

private static boolean needsKitKatContentUrlFix(Uri uri) {
    return android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT
            && "content".equals(uri.getScheme());
}

From source file:com.iceteck.silicompressorr.FileUtils.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>//from w  w w . jav  a 2 s  .  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.
 * @see #isLocal(String)
 * @see #getFile(Context, Uri)
 * @author paulburke
 */
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 (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];
            }

            // 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.appunite.websocket.WebSocket.java

/**
 * get websocket port from uri (thread safe)
 * // w  w w . ja  va 2  s . c om
 * @param uri uri to get port from
 * @return port number
 * @throws IllegalArgumentException
 *             if unknwon schema
 */
private static int getPort(Uri uri) {
    int port = uri.getPort();
    if (port != -1)
        return port;

    String scheme = uri.getScheme();
    if ("wss".equals(scheme)) {
        return DEFAULT_WSS_PORT;
    } else if ("ws".equals(scheme)) {
        return DEFAULT_WS_PORT;
    } else {
        throw new IllegalArgumentException("Unknown schema");
    }
}

From source file:mil.nga.giat.mage.sdk.utils.MediaUtility.java

public static String getFileAbsolutePath(Uri uri, Context c) {
    String fileName = null;//from  w w  w. j av a  2 s. c om
    String scheme = uri.getScheme();
    if (scheme.equals("file")) {
        fileName = uri.getPath();
    } else if (scheme.equals("content")) {
        Cursor cursor = null;
        try {
            String[] proj = { MediaStore.Images.Media.DATA };
            cursor = c.getContentResolver().query(uri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } catch (Exception e) {
            Log.e(LOG_NAME, "Error reading content URI", e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
    return fileName;
}