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

/**
 * Try to return the absolute file path from the given Uri
 * /*from w  ww.  j a  v a  2 s  .co m*/
 * @param context
 * @param uri
 * @return the file path or null
 */
public static String getRealFilePath(final Context context, final Uri uri) {

    if (null == uri)
        return null;

    final String scheme = uri.getScheme();
    String data = null;

    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

/**
 * Return the filename from a uri.//from  w  w w .j  a  v  a  2 s.c o  m
 */
public static String getFilename(Context c, Uri uri) {
    try {
        String scheme = uri.getScheme();
        if (scheme.equals("file")) {
            return uri.getLastPathSegment();
        } else if (scheme.equals("content")) {
            String[] proj = { MediaStore.Files.FileColumns.DISPLAY_NAME };
            Cursor cursor = c.getContentResolver().query(uri, proj, null, null, null);
            if (cursor != null && cursor.getCount() != 0) {
                int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DISPLAY_NAME);
                cursor.moveToFirst();
                return cursor.getString(columnIndex);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String getPath(Context context, Uri uri) {
    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor;/*from  ww  w .java2s.c  o m*/

        try {
            cursor = context.getContentResolver().query(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndex("_data");
            if (column_index != -1 && cursor.moveToFirst()) {
                String path = cursor.getString(column_index);
                if (path == null) {
                    path = getNewTemporaryFilePath(context, uri);
                }
                return path;
            } else {
                return getNewTemporaryFilePath(context, uri);
            }
        } catch (Exception e) {
            return getNewTemporaryFilePath(context, uri);
        }
    } else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

From source file:Main.java

/**
 * Try to return the absolute file path from the given Uri
 *
 * @param context/*from  w  w w  .j  a  v a  2s. co m*/
 * @param uri
 * @return the file path or null
 */
public static String uri2FilePath(final Context context, final Uri uri) {
    if (null == uri)
        return null;
    final String scheme = uri.getScheme();
    String data = null;
    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[] { MediaStore.Images.ImageColumns.DATA }, null, null, null);
        if (null != cursor) {
            if (cursor.moveToFirst()) {
                int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
                if (index > -1) {
                    data = cursor.getString(index);
                }
            }
            cursor.close();
        }
    }
    return data;
}

From source file:Main.java

/**
 * Return an {@link InputStream} from the given uri. ( can be a local
 * content, a file path or an http url )
 *
 * @param context/*from   ww  w  .j a  va  2s. c om*/
 * @param uri
 * @return the {@link InputStream} from the given uri, null if uri cannot be
 *         opened
 */
public static InputStream openInputStream(Context context, Uri uri) {
    if (null == uri)
        return null;
    final String scheme = uri.getScheme();
    InputStream stream = null;
    if (scheme == null || ContentResolver.SCHEME_FILE.equals(scheme)) {
        // from file
        stream = openFileInputStream(uri.getPath());
    } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
        // from content
        stream = openContentInputStream(context, uri);
    }
    return stream;
}

From source file:Main.java

public static InputStream getInputStream(Context context, Uri uri) throws IOException {
    InputStream inputStream;/*from   ww w.  j  a  v  a 2  s. c om*/
    if (ContentResolver.SCHEME_ANDROID_RESOURCE.equals(uri.getScheme())
            || ContentResolver.SCHEME_ANDROID_RESOURCE.equals(uri.getScheme())
            || ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
        inputStream = context.getContentResolver().openInputStream(uri);
    } else {
        URLConnection urlConnection = new URL(uri.toString()).openConnection();
        urlConnection.setConnectTimeout(URLCONNECTION_CONNECTION_TIMEOUT_MS);
        urlConnection.setReadTimeout(URLCONNECTION_READ_TIMEOUT_MS);
        inputStream = urlConnection.getInputStream();
    }
    return new BufferedInputStream(inputStream);
}

From source file:Main.java

public static File getFromMediaUri(ContentResolver resolver, Uri uri) {
    if (uri == null)
        return null;

    if (SCHEME_FILE.equals(uri.getScheme())) {
        return new File(uri.getPath());
    } else if (SCHEME_CONTENT.equals(uri.getScheme())) {
        final String[] filePathColumn = { MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DISPLAY_NAME };
        Cursor cursor = null;//from   w  w  w.j av  a2  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);
                // Picasa image on newer devices with Honeycomb and up
                if (columnIndex != -1) {
                    String filePath = cursor.getString(columnIndex);
                    if (!TextUtils.isEmpty(filePath)) {
                        return new File(filePath);
                    }
                }
            }
        } catch (SecurityException ignored) {
            // Nothing we can do
        } finally {
            if (cursor != null)
                cursor.close();
        }
    }
    return null;
}

From source file:Main.java

public static InputStream openInputStream(Context context, Uri uri) {
    if (null == uri)
        return null;
    String scheme = uri.getScheme();
    InputStream stream = null;//from   w ww .ja va 2 s . c o m
    if ((scheme == null) || ("file".equals(scheme))) {
        stream = openFileInputStream(uri.getPath());
    } else if ("content".equals(scheme)) {
        stream = openContentInputStream(context, uri);
    } else if (("http".equals(scheme)) || ("https".equals(scheme))) {
        stream = openRemoteInputStream(uri);
    }
    return stream;
}

From source file:Main.java

/** @see http://stackoverflow.com/questions/477572/android-strange-out-of-memory-issue */
public static int getSampleSize(Context context, Uri uri, float maxSize) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;// ww w.j av  a2 s  . co m
    if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
        try {
            InputStream stream = context.getContentResolver().openInputStream(uri);
            BitmapFactory.decodeStream(stream, null, options);
            stream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else
        BitmapFactory.decodeFile(uri.getPath(), options);
    int longSide = Math.max(options.outHeight, options.outWidth);
    return getSampleSize(longSide, maxSize);
}

From source file:org.andstatus.app.util.UriUtils.java

public static boolean isDownloadable(Uri uri) {
    if (uri != null) {
        String scheme = uri.getScheme();
        if (scheme != null) {
            switch (scheme) {
            case "http":
            case "https":
                return true;
            default:
                break;
            }//from  w  ww.  j  av  a2 s .co m
        }
    }
    return false;
}