Example usage for android.content Context getContentResolver

List of usage examples for android.content Context getContentResolver

Introduction

In this page you can find the example usage for android.content Context getContentResolver.

Prototype

public abstract ContentResolver getContentResolver();

Source Link

Document

Return a ContentResolver instance for your application's package.

Usage

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  a  va 2 s . c om

        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:com.lemi.mario.download.utils.Proxy.java

/**
 * Return the proxy host set by the user.
 * //www  . j  a  v a  2 s  .  c  o  m
 * @param ctx A Context used to get the settings for the proxy host.
 * @return String containing the host name. If the user did not set a host
 *         name it returns the default host. A null value means that no
 *         host is to be used.
 */
static final public String getHost(Context ctx) {
    ContentResolver contentResolver = ctx.getContentResolver();
    Assert.assertNotNull(contentResolver);
    String host = Settings.Secure.getString(contentResolver, Settings.Secure.HTTP_PROXY);
    if (host != null) {
        int i = host.indexOf(':');
        if (i == -1) {
            if (DEBUG) {
                Assert.assertTrue(host.length() == 0);
            }
            return null;
        }
        return host.substring(0, i);
    }
    return getDefaultHost();
}

From source file:com.example.facebook_photo.Utility.java

public static int getOrientation(Context context, Uri photoUri) {
    /* it's on the external media. */
    Cursor cursor = context.getContentResolver().query(photoUri,
            new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);

    if (cursor.getCount() != 1) {
        return -1;
    }/*from   w  w  w. j a v  a2 s  .c o  m*/

    cursor.moveToFirst();
    int orientation = cursor.getInt(0);
    cursor.close();

    return orientation;
}

From source file:Main.java

public static Cursor query(Context context, Uri uri, String[] projection, String selection,
        String[] selectionArgs, String sortOrder, int limit) {
    try {/*  w w  w  .j  a  v a 2  s . co m*/
        ContentResolver resolver = context.getContentResolver();
        if (resolver == null) {
            return null;
        }
        if (limit > 0) {
            uri = uri.buildUpon().appendQueryParameter("limit", "" + limit).build();
        }
        return resolver.query(uri, projection, selection, selectionArgs, sortOrder);
    } catch (UnsupportedOperationException ex) {
        return null;
    }

}

From source file:Main.java

/**
 * Returns a BitmapFactory.Options object containing the size of the image at the given URI,
 * without actually loading the image./*from   w  ww . ja  va 2  s  .co m*/
 */
public static BitmapFactory.Options computeBitmapSizeFromURI(Context context, Uri imageURI)
        throws FileNotFoundException {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    Bitmap bitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(imageURI), null,
            options);
    if (bitmap != null) {
        bitmap.recycle();
    }
    return options;
}

From source file:Main.java

public static boolean isOnlyGpsLocationModeEnabled(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        int locationMode;

        try {/*from w ww  .  j  a  v a 2  s .  c  om*/
            locationMode = Settings.Secure.getInt(context.getContentResolver(), Settings.Secure.LOCATION_MODE);

        } catch (Settings.SettingNotFoundException e) {
            return false;
        }

        // GPS must be enabled
        return Settings.Secure.LOCATION_MODE_SENSORS_ONLY == locationMode
                || Settings.Secure.LOCATION_MODE_HIGH_ACCURACY == locationMode;

    } else {
        String locationProviders = Settings.Secure.getString(context.getContentResolver(),
                Settings.Secure.LOCATION_PROVIDERS_ALLOWED);

        if (TextUtils.isEmpty(locationProviders)) {
            return false;
        }

        String[] providers = locationProviders.split(",");

        for (String provider : providers) {
            // GPS must be enabled
            if (provider.equals(LocationManager.GPS_PROVIDER)) {
                return true;
            }
        }

        return false;
    }
}

From source file:com.lemi.mario.download.utils.Proxy.java

/**
 * Return the proxy port set by the user.
 * // w ww .  jav  a 2 s . com
 * @param ctx A Context used to get the settings for the proxy port.
 * @return The port number to use or -1 if no proxy is to be used.
 */
static final public int getPort(Context ctx) {
    ContentResolver contentResolver = ctx.getContentResolver();
    Assert.assertNotNull(contentResolver);
    String host = Settings.Secure.getString(contentResolver, Settings.Secure.HTTP_PROXY);
    if (host != null) {
        int i = host.indexOf(':');
        if (i == -1) {
            if (DEBUG) {
                Assert.assertTrue(host.length() == 0);
            }
            return -1;
        }
        if (DEBUG) {
            Assert.assertTrue(i < host.length());
        }
        return Integer.parseInt(host.substring(i + 1));
    }
    return getDefaultPort();
}

From source file:ee.ria.DigiDoc.util.FileUtils.java

public static File cacheUriAsDataFile(Context context, Uri uri) {
    String fileName = resolveFileName(uri, context.getContentResolver());
    return cacheUriAsDataFile(context, uri, fileName);
}

From source file:ee.ria.DigiDoc.util.FileUtils.java

public static File uriAsContainerFile(Context context, Uri uri) {
    String fileName = resolveFileName(uri, context.getContentResolver());
    return uriAsContainerFile(context, uri, fileName);
}

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 ww w  .j a va  2s.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());
}