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:Main.java

public static boolean isAttach(Uri uriFromIntent) {
    return (uriFromIntent != null) && (uriFromIntent.getScheme().contains("content"));
}

From source file:Main.java

@Nullable
public static Uri safeUri(@NonNull String url) {
    if (TextUtils.isEmpty(url)) {
        return null;
    }//w  w w  .  jav a2 s . c  om

    Uri uri = Uri.parse(url);

    if (TextUtils.isEmpty(uri.getScheme()) || TextUtils.isEmpty(uri.getHost())) {
        return null;
    }

    return uri;
}

From source file:Main.java

/**
 * Acquires input stream for the image resource identified by uri.
 *
 * This is a long-running I/O operation that must run in a background thread.
 *
 * @param context// w  ww  . ja v a2s .c  om
 * @param uri
 * @return
 * @throws IOException
 */
public static InputStream getInputStream(Context context, Uri uri) throws IOException {
    if (uri.getScheme().contentEquals(ContentResolver.SCHEME_CONTENT)) {
        return context.getContentResolver().openInputStream(uri);
    } else {
        return (InputStream) new URL(uri.toString()).getContent();
    }
}

From source file:Main.java

/**
 * Converts the uri to an InputStream.//from w  ww.  j a v  a  2  s. c  o m
 *
 * @param context Context used to resolve file and content.
 * @param uri     Uri to convert.
 * @return InputStream or null if something went wrong.
 */
private static InputStream toInputStream(Context context, Uri uri) {
    String scheme = uri.getScheme();
    if (scheme.equals("http") || scheme.equals("https")) {
        try {
            return new URL(uri.getPath()).openConnection().getInputStream();
        } catch (NullPointerException e) {
            return null;
        } catch (MalformedURLException e) {
            return null;
        } catch (IOException e) {
            return null;
        }
    } else if (scheme.equals("file") || scheme.equals("content")) {
        try {
            return context.getContentResolver().openInputStream(uri);
        } catch (FileNotFoundException e) {
            return null;
        }
    }
    return null;
}

From source file:Main.java

/**
 * Utils to get File path//from  w  w w .jav  a 2s  .  co  m
 * 
 * @param uri
 * @return
 */
public static String getPath(Context context, Uri uri) {
    String scheme = uri.getScheme();
    String s = null;
    if (scheme.equals("content")) {
        String[] projection = { MediaStore.Files.FileColumns.DATA };
        Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
        int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA);
        cursor.moveToFirst();
        s = cursor.getString(columnIndex);
    } else if (scheme.equals("file")) {
        s = uri.getPath();
    }
    // Log.d("ActionManager", "URI:" + uri + " - S:" + s);
    return s;
}

From source file:Main.java

public static String buildAccountName(Uri serverBaseUrl, String username) {
    if (serverBaseUrl.getScheme() == null) {
        serverBaseUrl = Uri.parse("https://" + serverBaseUrl.toString());
    }//from   w  w  w.  j a  v  a2  s  .c  om
    String accountName = username + "@" + serverBaseUrl.getHost();
    if (serverBaseUrl.getPort() >= 0) {
        accountName += ":" + serverBaseUrl.getPort();
    }
    return accountName;
}

From source file:Main.java

public static String buildAccountNameOld(Uri serverBaseUrl, String username) {
    if (serverBaseUrl.getScheme() == null) {
        serverBaseUrl = Uri.parse("https://" + serverBaseUrl.toString());
    }/*from   w w w  . ja va2 s.com*/
    String accountName = username + "@" + serverBaseUrl.getHost();
    if (serverBaseUrl.getPort() >= 0) {
        accountName += ":" + serverBaseUrl.getPort();
    }
    return accountName;
}

From source file:Main.java

/**
 * @param uri uri to extract scheme from, possibly null
 * @return null if uri is null, result of uri.getScheme() otherwise
 *///from  w  w  w .ja va  2  s .c  o m
@Nullable
public static String getSchemeOrNull(@Nullable Uri uri) {
    return uri == null ? null : uri.getScheme();
}

From source file:Main.java

@Nullable
static Uri safeUri(@NonNull String url) {
    if (TextUtils.isEmpty(url)) {
        return null;
    }// w w  w.ja  v  a 2s  .c o m

    Uri uri = Uri.parse(url);

    if (uri.getHost() == null || uri.getScheme() == null) {
        return null;
    }

    return uri;
}

From source file:Main.java

public static boolean isMediaStoreUri(Uri uri) {
    return uri != null && ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())
            && MediaStore.AUTHORITY.equals(uri.getAuthority());
}