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 getRealPathByUri(Context context, Uri uri) {
    if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
        return uri.getPath();
    }//from  w  w  w .  j a va 2  s .c  o  m

    try {
        ContentResolver resolver = context.getContentResolver();
        String[] proj = new String[] { MediaStore.Images.Media.DATA };
        Cursor cursor = MediaStore.Images.Media.query(resolver, uri, proj);
        String realPath = null;
        if (cursor != null) {
            int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            if (cursor.getCount() > 0 && cursor.moveToFirst()) {
                realPath = cursor.getString(columnIndex);
            }
            cursor.close();
        }
        return realPath;
    } catch (Exception e) {
        return uri.getPath();
    }
}

From source file:Main.java

/**
 * Calculates the amount of rotation needed for the image to look upright.
 * /*from   w  ww. j  a  v a  2 s. c om*/
 * @param context
 *            the application's context
 * @param uri
 *            the Uri pointing to the file
 * @return the needed rotation as a <code>float</code>
 */
private static float rotationForImage(Context context, Uri uri) {
    if ("content".equals(uri.getScheme())) {
        String[] projection = { Images.ImageColumns.ORIENTATION };
        Cursor c = context.getContentResolver().query(uri, projection, null, null, null);
        if (c.moveToFirst()) {
            return c.getInt(0);
        }
    } else if ("file".equals(uri.getScheme())) {
        try {
            ExifInterface exif = new ExifInterface(uri.getPath());
            int rotation = (int) exifOrientationToDegrees(
                    exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL));
            return rotation;
        } catch (IOException e) {
            Log.e(TAG, "Error checking exif", e);
        }
    }
    return 0f;
}

From source file:Main.java

public static boolean isOnlineVideo(Uri uri) {
    if (uri == null) {
        return false;
    }/*from w  w w . ja v a2s  .  c  o m*/
    String scheme = uri.getScheme();
    if (scheme != null && (scheme.equals("http") || scheme.equals("https") || scheme.equals("rtsp"))) {
        return true;
    }
    return false;
}

From source file:Main.java

public static String getPath(Context context, Uri uri) throws URISyntaxException {
    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;//from w  ww. j ava 2 s.co  m

        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

public static String getFileName(Uri uri, Context context) {
    String result = null;/*from  ww  w.  jav  a 2s.  c  o  m*/
    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));
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
    if (result == null) {
        result = uri.getPath();
        int cut = result.lastIndexOf('/');
        if (cut != -1) {
            result = result.substring(cut + 1);
        }
    }
    return result;
}

From source file:Main.java

public static boolean isRtspVideo(Uri uri) {
    if (uri == null) {
        return false;
    }/* w  ww .  jav a2  s.  c  om*/
    String scheme = uri.getScheme();
    if (scheme != null && scheme.equalsIgnoreCase("rtsp")) {
        return true;
    } else {
        return false;
    }
}

From source file:Main.java

public static String getPathFromUri(Uri uri) {
    if (uri == null) {
        return null;
    }/*from w ww.j  av  a 2s  .c  om*/
    String scheme = uri.getScheme();
    if (scheme != null && scheme.equals("file")) {
        String filePath = uri.getPath();
        return filePath;
    }
    return null;

}

From source file:Main.java

public static boolean isOnlineVideo(Uri uri) {
    if (uri == null) {
        return false;
    }/*  www. ja  v a2 s  . c  o  m*/
    String scheme = uri.getScheme();
    if (scheme != null && (scheme.equals("http") || scheme.equals("https") || scheme.equals("rtsp"))) {
        return true;
    }
    String path = uri.toString();
    if (path != null && path.contains("app_smb")) {
        return true;
    }
    return false;
}

From source file:Main.java

/**
 * Get a uri's user-friendly display name
 * //from   www  .j  ava2s  .  c o  m
 * @param context the application context
 * @param uri     the uri to query
 * 
 * @return a user-friendly display name
 */
public static String getUriDisplayName(Context context, Uri uri) {
    String displayName = null;

    String scheme = uri.getScheme();

    if (scheme.startsWith("content")) {
        String[] proj = { OpenableColumns.DISPLAY_NAME };
        Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null);

        if (cursor != null) {
            int columnIndex = cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME);
            cursor.moveToFirst();
            displayName = cursor.getString(columnIndex);

            cursor.close();
        }
    } else if (scheme.startsWith("file")) {
        displayName = uri.getLastPathSegment();
    }

    return displayName;
}

From source file:Main.java

public static String getUriSafeString(Uri uri) {
    final String scheme = uri.getScheme();
    final String ssp = uri.getSchemeSpecificPart();

    if (scheme != null) {
        if (scheme.equalsIgnoreCase("tel") || scheme.equalsIgnoreCase("sip") || scheme.equalsIgnoreCase("sms")
                || scheme.equalsIgnoreCase("smsto") || scheme.equalsIgnoreCase("mailto")) {
            StringBuilder builder = new StringBuilder(64);
            builder.append(scheme);//from   w w  w .  j  a  v a2s. c o  m
            builder.append(':');

            if (ssp != null) {
                for (int i = 0; i < ssp.length(); i++) {
                    char c = ssp.charAt(i);

                    if (c == '-' || c == '@' || c == '.') {
                        builder.append(c);
                    } else {
                        builder.append('x');
                    }
                }
            }
            return builder.toString();
        }
    }

    StringBuilder builder = new StringBuilder(64);
    if (scheme != null) {
        builder.append(scheme);
        builder.append(':');
    }

    if (ssp != null) {
        builder.append(ssp);
    }
    return builder.toString();
}