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 int getMessageTypeByThreadAndBody(Context context, long threadId, String body) {
    int type = 1;
    final String[] projection = new String[] { " sms.type " + " from sms " + " where sms.thread_id = "
            + threadId + " and sms.body = '" + body + "' --" };
    // Create cursor
    /*// ww w.j  av a  2s. com
     * Cursor cursor = context.getContentResolver().query( SMS_CONTENT_URI,
     * projection, null, null, null);
     */
    Cursor cursor = context.getContentResolver().query(SMS_CONTENT_URI, new String[] { "type" },
            "sms.thread_id = ? and sms.body = ?", new String[] { String.valueOf(threadId), body }, null);

    if (cursor != null) {
        try {
            if (cursor.getCount() > 0) {

                while (cursor.moveToNext()) {
                    type = cursor.getInt(cursor.getColumnIndexOrThrow("type"));
                    return type;
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            cursor.close();
        }
    }

    return type;
}

From source file:Main.java

public static Long getOrCreateThreadId(Context context, String phone) {
    try {//from www.j  av a2s. c o m
        Uri threadIdUri = Uri.parse("content://mms-sms/threadID");
        Uri.Builder builder = threadIdUri.buildUpon();
        String[] recipients = { phone };
        for (String recipient : recipients) {
            builder.appendQueryParameter("recipient", recipient);
        }
        Uri uri = builder.build();
        Long threadId = 0L;
        Cursor cursor = context.getContentResolver().query(uri, new String[] { "_id" }, null, null, null);
        if (cursor != null) {
            try {
                if (cursor.moveToFirst()) {
                    threadId = cursor.getLong(0);
                }
            } finally {
                cursor.close();
            }
            return threadId;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return -1L;
}

From source file:im.delight.android.baselib.Social.java

/**
 * Whether the given person (represented by phone number) is known on the current device (i.e. in the address book) or not
 *
 * @param context the Context reference to get the ContentResolver from
 * @param phoneNumber the phone number to look up
 * @return whether the phone number is in the contacts list (true) or not (false)
 *///from  w w w  .j a  v  a 2  s  .  co m
public static boolean isPersonKnown(Context context, String phoneNumber) {
    try {
        Uri phoneUri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
                Uri.encode(phoneNumber));
        Cursor phoneEntries = context.getContentResolver().query(phoneUri,
                new String[] { android.provider.ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);
        return phoneEntries.getCount() > 0;
    } catch (Exception e) {
        return false;
    }
}

From source file:Main.java

/**
 * Returns a Bitmap from the given URI that may be scaled by an integer factor to reduce its size,
 * while staying as least as large as the width and height parameters.
 *///w  w  w  . j  a  v  a2  s  . c om
@Nullable
public static Bitmap scaledBitmapFromURIWithMinimumSize(Context context, Uri imageURI, int width, int height)
        throws FileNotFoundException {
    BitmapFactory.Options options = computeBitmapSizeFromURI(context, imageURI);
    options.inJustDecodeBounds = false;

    float wratio = 1.0f * options.outWidth / width;
    float hratio = 1.0f * options.outHeight / height;
    options.inSampleSize = (int) Math.min(wratio, hratio);

    return BitmapFactory.decodeStream(context.getContentResolver().openInputStream(imageURI), null, options);
    //        return BitmapFactory.decodeStream(context.getContentResolver().openInputStream(imageURI));
}

From source file:ee.ioc.phon.android.speak.Utils.java

/**
 * TODO: should we immediately return null if id = 0?
 *///from   www.  j av a2 s  .  c  o  m
public static String idToValue(Context context, Uri contentUri, String columnId, String columnUrl, long id) {
    String value = null;
    Cursor c = context.getContentResolver().query(contentUri, new String[] { columnUrl }, columnId + "= ?",
            new String[] { String.valueOf(id) }, null);

    if (c.moveToFirst()) {
        value = c.getString(0);
    }
    c.close();
    return value;
}

From source file:Main.java

/**
 * The following method creates a File from a given Bitmap.
 * /*  ww w  .j  a v a  2  s.c o  m*/
 * @param context
 *            - The application context.
 * @param bitmap
 *            - The bitmap to be converted into a File.
 * @return
 */
public static File getFilefromBitmap(Context context, Bitmap bitmap) {
    File file = null;
    try {
        String path = Environment.getExternalStorageDirectory().toString();
        OutputStream fOut = null;
        file = new File(path, "1" + ".jpg");
        fOut = new FileOutputStream(file);

        bitmap.compress(Bitmap.CompressFormat.PNG, 100, fOut);
        fOut.flush();
        fOut.close();
        MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(),
                file.getName(), file.getName());
    } catch (Exception e) {
        e.printStackTrace();
    }
    return file;

}

From source file:Main.java

/**
 * Returns a bitmap from a gallery Uri/*ww  w  . ja  v a 2 s.  c o  m*/
 *
 * @param pContext
 *        Context required to access the content resolver
 * @param pIntent
 *        The Uri of the picker image
 * @return The picked image as a bitmap
 */
public static Bitmap getBitmapFromIntent(Context pContext, Intent pIntent) {
    Bitmap bitmapPickedImage = null;

    Uri pickedImageUri = pIntent.getData();

    // If the URI is not null try to decode it to a bitmap else try to get the bitmap data from the intent
    // http://stackoverflow.com/questions/17123083/null-pointer-exception-while-taking-pictures-from-camera-android-htc
    if (pickedImageUri != null) {
        try {
            InputStream imageStream = pContext.getContentResolver().openInputStream(pickedImageUri);
            bitmapPickedImage = BitmapFactory.decodeStream(imageStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    } else {
        if (pIntent.getExtras() != null && pIntent.getExtras().get("data") instanceof Bitmap) {
            bitmapPickedImage = (Bitmap) pIntent.getExtras().get("data");
        }
    }

    return bitmapPickedImage;
}

From source file:Main.java

public static boolean isShortCutExist(Context context) {
    boolean isExist = false;
    int version = android.os.Build.VERSION.SDK_INT;
    Uri uri = null;/* w ww  . jav a  2 s.  c o  m*/
    if (version < 2.0) {
        uri = Uri.parse("content://com.android.launcher.settings/favorites");
    } else {
        uri = Uri.parse("content://com.android.launcher2.settings/favorites");
    }
    String selection = " title = ?";
    String[] selectionArgs = new String[] { "YouTube" };
    Cursor c = context.getContentResolver().query(uri, null, selection, selectionArgs, null);

    if (c != null && c.getCount() > 0) {
        isExist = true;
    }

    if (c != null) {
        c.close();
    }

    return isExist;
}

From source file:com.google.android.apps.chrometophone.DeviceRegistrar.java

private static HttpResponse makeRequest(Context context, String deviceRegistrationID, String urlPath)
        throws Exception {
    String accountName = getAccountName(context);

    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("devregid", deviceRegistrationID));

    String deviceId = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
    if (deviceId != null) {
        params.add(new BasicNameValuePair("deviceId", deviceId));
    }/*from  ww w.  jav a2s  .c  o  m*/

    // TODO: Allow device name to be configured
    params.add(new BasicNameValuePair("deviceName", isTablet(context) ? "Tablet" : "Phone"));

    params.add(new BasicNameValuePair("deviceType", "ac2dm"));
    params.add(new BasicNameValuePair("gcm", "true"));

    AppEngineClient client = new AppEngineClient(context, accountName);
    return client.makeRequest(urlPath, params);
}

From source file:com.granita.icloudcalsync.resource.LocalTaskList.java

public static boolean isAvailable(Context context) {
    try {//from  w w  w  . j a  va2  s.c o m
        @Cleanup("release")
        ContentProviderClient client = context.getContentResolver()
                .acquireContentProviderClient(TASKS_AUTHORITY);
        return client != null;
    } catch (SecurityException e) {
        Log.e(TAG, "Sync for iCloud is not allowed to access tasks", e);
        return false;
    }
}