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 String getPath(Context context, Uri uri) {

    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;/*  ww  w  .j  av  a 2 s  . com*/

        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) {

        }
    }

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

    return null;
}

From source file:Main.java

/**
 * Return file size from Uri//w w w .jav a 2  s  .  c  o  m
 *
 * @param uri file URI
 * @return return file size
 */
public static long getFileSizeFromUri(Context context, Uri uri) {
    long size = 0;
    if (uri.getScheme().toString().compareTo("content") == 0) {
        Cursor cursor = null;
        try {
            cursor = context.getApplicationContext().getContentResolver().query(uri, null, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                final int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.SIZE);
                size = cursor.getInt(column_index);
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    } else if (uri.getScheme().toString().compareTo("file") == 0) {
        final File file = new File(uri.getPath());
        size = file.length();
    }
    return size;
}

From source file:Main.java

public static String buildAccountName(Uri serverBaseUrl, String username) {
    if (serverBaseUrl.getScheme() == null) {
        serverBaseUrl = Uri.parse("https://" + serverBaseUrl.toString());
    }/*from  ww w  .j  a  va  2 s .  co  m*/

    // Remove http:// or https://
    String url = serverBaseUrl.toString();
    if (url.contains("://")) {
        url = url.substring(serverBaseUrl.toString().indexOf("://") + 3);
    }
    String accountName = username + "@" + url;

    return accountName;
}

From source file:Main.java

public static String pathFromUri(Uri fileUri) {
    String filePath = null;/*from   ww w.  j a v a  2  s.c om*/
    if (fileUri != null && "file".equals(fileUri.getScheme())) {
        filePath = fileUri.toString().substring("file://".length());
    }
    return filePath;
}

From source file:Main.java

public static String uriTitle(Context context, String contentUri) {
    String result = null;// ww  w  .j av  a  2 s . c  om
    String[] p = { MediaStore.MediaColumns.TITLE };
    Uri uri = Uri.parse(contentUri);
    if (!uri.getScheme().equals("content")) {
        return (fileUriTitle(context, contentUri));
    }
    Cursor cursor = context.getContentResolver().query(uri, p, // which columns
            null, // which rows (all rows)
            null, // selection args (none)
            null); // order-by clause (ascending by name)
    if (cursor != null) {
        int iColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.TITLE);
        if (cursor.moveToFirst()) {
            result = cursor.getString(iColumn);
        }
    }
    return (result);
}

From source file:Main.java

public static long uriFileSize(Context context, String contentUri) {
    long result = 0;
    Uri uri = Uri.parse(contentUri);
    if (!uri.getScheme().equals("content")) {
        return (fileUriFileSize(context, contentUri));
    }/*from  w  ww.j a v a  2  s . c o  m*/
    String[] p = { MediaStore.MediaColumns.SIZE };
    Cursor cursor = context.getContentResolver().query(uri, p, // which columns
            null, // which rows (all rows)
            null, // selection args (none)
            null); // order-by clause (ascending by name)
    if (cursor != null) {
        int iColumn = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.SIZE);
        if (cursor.moveToFirst()) {
            result = cursor.getLong(iColumn);
        }
    }
    return (result);
}

From source file:Main.java

/**
 * Generate an input stream reading from an android URI
 * //w w w . ja va2 s  .c  o m
 * @param uri
 * @return
 * @throws IOException
 */
public static InputStream getFromURI(Context context, Uri uri) throws IOException {

    if (uri.getScheme().equals("content"))
        return context.getContentResolver().openInputStream(uri);
    else if (uri.getScheme().equals("file")) {
        URL url = new URL(uri.toString());

        return url.openStream();
    } else {
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(uri.toString());
        HttpResponse response = httpclient.execute(httpget);
        HttpEntity entity = response.getEntity();
        if (entity != null)
            return entity.getContent();
        else
            throw new IOException("No HTTP response");
        // Use the regular java stuff
        // URL url = new URL(uri.toString());

        // return url.openStream();
    }
}

From source file:Main.java

static String getFileName(Context context, Uri uri) {
    String result = null;// w  w  w  .  j  av  a  2s . com
    if (uri.getScheme().equals("content")) {
        Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
        try {
            if (cursor != null && cursor.moveToFirst()) {
                result = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
    if (result == null) {
        result = uri.getPath();
        int cut = result.lastIndexOf(File.separator);
        if (cut != -1) {
            result = result.substring(cut + 1);
        }
    }
    return result;
}

From source file:Main.java

/**
 * Return file display name from Uri//from   ww  w .  j  a  v  a 2  s. co  m
 *
 * @param context Context
 * @param uri URI of the file
 * @return return file display name
 */
public static String getFileDisplayNameFromUri(Context context, Uri uri) {
    String displayName = "";
    if (uri.getScheme().toString().compareTo("content") == 0) {
        Cursor cursor = null;
        try {
            cursor = context.getApplicationContext().getContentResolver().query(uri, null, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                final int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DISPLAY_NAME);
                displayName = cursor.getString(column_index);
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    } else if (uri.getScheme().toString().compareTo("file") == 0) {
        final File file = new File(uri.getPath());
        displayName = file.getName();
    }
    return displayName;
}

From source file:Main.java

public static InputStream openStreamForUri(Context context, Uri uri) throws IOException {
    switch (uri.getScheme()) {
    case ContentResolver.SCHEME_CONTENT:
    case ContentResolver.SCHEME_ANDROID_RESOURCE:
        try {/*from  www  .  j  a  v a  2  s .c  o  m*/
            return context.getContentResolver().openInputStream(uri);
        } catch (RuntimeException ignore) {
        }
    case "file":
    case "http":
    case "https":
        return new URL(uri.toString()).openStream();
    }

    throw new IOException("Unable to open " + uri.toString());
}