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

@Nullable
private static InputStream getStreamFromUri(Uri selectedImageURI, Context theContext) throws IOException {
    switch (selectedImageURI.getScheme()) {
    case "content":
        return theContext.getContentResolver().openInputStream(selectedImageURI);

    case "file":
        return new FileInputStream(new File(URI.create(selectedImageURI.toString())));

    case "http":
        // Fall through
    case "https":
        final URL url = new URL(selectedImageURI.toString());
        final HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);/*  www. ja v a2s  . co  m*/
        connection.connect();
        return connection.getInputStream();

    default:
        Log.w(TAG, "getStreamFromUri(): unsupported Uri scheme: " + selectedImageURI.getScheme());
        return null;
    }
}

From source file:Main.java

/**
 * Get a uri's file path//from  w  w w  .j  a  v a2 s.  c o m
 * 
 * @param context the application context
 * @param uri     the uri to query
 * 
 * @return the file path
 */
public static String getUriPath(Context context, Uri uri) {
    String filePath = null;

    String scheme = uri.getScheme();

    if (scheme.startsWith("content")) {
        String[] projection = { MediaStore.Files.FileColumns.DATA };

        /* 
         * FIXME 2013-10-24 Tianzi Hou
         * 
         * we cannot get file path if it is from 
         *  content://com.google.android.gallery3d.provider
         * i.e. the Picasa service
         */
        filePath = null;
        Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);

        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(projection[0]);
            cursor.moveToFirst();
            filePath = cursor.getString(column_index);
            cursor.close();
        }
    } else if (scheme.startsWith("file")) {
        filePath = uri.getPath();
    }

    return filePath;
}

From source file:Main.java

public static String getRealFilePath(Context context, Uri uri) {
    if (null == uri)
        return null;
    String scheme = uri.getScheme();
    String data = null;// ww  w . j  a  va  2 s  . co m
    if (scheme == null)
        data = uri.getPath();
    else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
        data = uri.getPath();
    } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
        Cursor cursor = context.getContentResolver().query(uri, new String[] { ImageColumns.DATA }, null, null,
                null);
        if (null != cursor) {
            if (cursor.moveToFirst()) {
                int index = cursor.getColumnIndex(ImageColumns.DATA);
                if (index > -1) {
                    data = cursor.getString(index);
                }
            }
            cursor.close();
        }
    }
    return data;
}

From source file:Main.java

public static File getFileFromUri(Uri uri, Activity activity) {
    String filePath = null;/*from www. j a v a2s  .  c  o m*/
    String scheme = uri.getScheme();
    filePath = uri.getPath();
    if (filePath != null && scheme != null && scheme.equals("file")) {
        return new File(filePath);
    }

    String[] projection = { MediaStore.Images.ImageColumns.DATA };
    Cursor c = activity.managedQuery(uri, projection, null, null, null);
    if (c != null && c.moveToFirst()) {
        filePath = c.getString(0);
    }
    if (filePath != null) {
        return new File(filePath);
    }
    return null;

}

From source file:Main.java

protected static String getImagePath(Uri imageUri, Context context) {
    String scheme = imageUri.getScheme();
    if (scheme.equals("file"))
        return imageUri.getPath();
    Cursor cursor = null;//from  w  w  w  .  jav a 2  s  .c  o m
    try {
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(imageUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null)
            cursor.close();
    }

}

From source file:Main.java

/**
 * Query the server app to get the file's display name.
 *//*from w w w.j  a va2s  .  c o m*/
public static String getUriFileName(Context context, Uri uri) {
    if (uri == null) {
        return null;
    }

    if (uri.getScheme().equals("file")) {
        return uri.getLastPathSegment();
    }

    if (uri.getScheme().equals("content")) {
        Cursor returnCursor = context.getContentResolver().query(uri, null, null, null, null);

        //Get the column index of the data in the Cursor,
        //move to the first row in the Cursor, get the data, and display it.
        int name_index = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
        //int size_index = returnCursor.getColumnIndex(OpenableColumns.SIZE);

        if (name_index < 0) {
            return null;
        }

        returnCursor.moveToFirst();

        //return returnCursor.getLong(size_index)
        return returnCursor.getString(name_index);
    }
    return null;
}

From source file:Main.java

public static File getFromMediaUri(ContentResolver resolver, Uri uri) {
    if (uri == null)
        return null;
    if ("file".equals(uri.getScheme())) {
        return new File(uri.getPath());
    } else if ("content".equals(uri.getScheme())) {
        final String[] filePathColumn = { MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME };
        Cursor cursor = null;/*from  w w w  . ja  v  a 2  s.  c  o  m*/
        try {
            cursor = resolver.query(uri, filePathColumn, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                final int columnIndex = (uri.toString().startsWith("content://com.google.android.gallery3d"))
                        ? cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME)
                        : cursor.getColumnIndex(MediaStore.MediaColumns.DATA);
                if (columnIndex != -1) {
                    String filePath = cursor.getString(columnIndex);
                    if (!TextUtils.isEmpty(filePath)) {
                        return new File(filePath);
                    }
                }
            }
        } catch (SecurityException ignored) {
        } finally {
            if (cursor != null)
                cursor.close();
        }
    }
    return null;
}

From source file:Main.java

public static boolean uriHasSchema(String uri) {
    if (uri == null || uri.length() == 0) {
        return false;
    }/*from w w  w .jav  a2s  .  c  om*/
    String mUri = null;
    int i = uri.indexOf('?');
    if (i > 0) {
        mUri = uri.substring(0, i);
    } else {
        mUri = uri;
    }
    final Uri path = Uri.parse(mUri);
    if (path != null && path.getScheme() != null) {
        return true;
    } else {
        return false;
    }
}

From source file:Main.java

public static void shiftTableUriIds(HashMap<String, ArrayList<ContentValues>> operationMap, String tableName,
        String idColumnName, String authority, String path, long topTableId) {
    ArrayList<ContentValues> restoreOperations = operationMap.get(tableName);
    if (null == restoreOperations) {
        return;//  w w w . j a  va  2  s.  co  m
    }
    for (ContentValues restoreCv : restoreOperations) {
        if (restoreCv.containsKey(idColumnName) && null != restoreCv.getAsString(idColumnName)) {

            Uri uri = Uri.parse(restoreCv.getAsString(idColumnName));
            if ("content".equalsIgnoreCase(uri.getScheme()) && authority.equals(uri.getAuthority())
                    && uri.getPath().substring(0, uri.getPath().lastIndexOf('/')).equals(path)) {
                Uri.Builder newUri = uri.buildUpon()
                        .path(uri.getPath().substring(0, uri.getPath().lastIndexOf('/')))
                        .appendPath(String.valueOf(
                                Long.parseLong(uri.getPath().substring(uri.getPath().lastIndexOf('/') + 1))
                                        + topTableId));

                //               Log.v("URI", uri.getPath().substring(uri.getPath().lastIndexOf('/')+1));
                //               Log.v("URI", String.valueOf(Long.parseLong(uri.getPath().substring(uri.getPath().lastIndexOf('/')+1)) + topTableId));

                restoreCv.put(idColumnName, newUri.build().toString());
            }
        }
    }
}

From source file:Main.java

public static String makeSpecUrl(String inBaseUrl) {
    Uri path = Uri.parse(inBaseUrl);
    String port = path.getPort() != -1 ? ":" + String.valueOf(path.getPort()) : "";
    return path.getScheme() + "://" + path.getHost() + port + path.getPath();
}