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 getMessageCount(Context context, String id) {
    String res = null;/*from ww  w. j  ava  2  s  . com*/
    try {
        final String[] projection = new String[] { "_id", "message_count" };
        Uri uri = Uri.parse("content://mms-sms/conversations?simple=true");
        Cursor query = context.getContentResolver().query(uri, projection, null, null, "date DESC");
        if (query != null) {
            boolean find = false;
            while (query.moveToNext() && !find) {
                if (query.getString(query.getColumnIndex("_id")).equals(id)) {
                    res = query.getString(query.getColumnIndex("message_count"));
                    //                        Log.v("getMessageCount", "find, nb_sms = "+res);
                    find = true;
                }
            }
            query.close();
        }
    } catch (Exception e) {
        //            Log.v("getMessageCount", "Erreur");
        e.printStackTrace();
    }
    return res;
}

From source file:Main.java

public static String getFirstCNLetterByContactId8(Context context, long contactId) {
    String result = "";
    String[] projection = { "_id", "display_name", "data1", "sort_key" };
    String where = Data.CONTACT_ID + "=?";
    final String sortOrder = null;
    Cursor cursor = context.getContentResolver().query(PHONE_CONTACTS_URI, projection, where,
            new String[] { String.valueOf(contactId) }, sortOrder);

    if (cursor != null) {
        try {//w  w w.  j  a va 2 s  .c om
            if (cursor.getCount() > 0) {
                while (cursor.moveToNext()) {
                    String nameLetters = cursor.getString(cursor.getColumnIndexOrThrow("sort_key"));
                    if (null != nameLetters && !"".equals(nameLetters)) {
                        result = nameLetters.substring(0, 1).toUpperCase();
                        break;
                    }
                }
            }

        } finally {
            cursor.close();
        }
    }

    return result;
}

From source file:Main.java

@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public static void toggleAirplane(Context context) {

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
        return;/*from   ww w.j a  v  a 2s  . c o m*/

    int state = 0;
    try {
        if (isJBean())
            state = Settings.System.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON);
        else
            state = Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON);
    } catch (SettingNotFoundException e) {
        e.printStackTrace();
    }

    if (state == 0) {
        if (isJBean())
            Settings.System.putInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
        else
            Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 1);
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        intent.putExtra("state", true);
        context.sendBroadcast(intent);
    } else {
        if (isJBean())
            Settings.System.putInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0);
        else
            Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0);
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        intent.putExtra("state", false);
        context.sendBroadcast(intent);
    }
}

From source file:Main.java

private static Bitmap readScaledBitmapFromUri(Uri photoUri, Context context, int width, int height)
        throws FileNotFoundException, IOException {
    Log.i("Photo Editor", "Read Scaled Bitmap: " + width + " " + height);
    InputStream is;/*  w w  w .j av a  2  s  . c om*/
    Bitmap srcBitmap;
    is = context.getContentResolver().openInputStream(photoUri);
    if (width > MAX_IMAGE_DIMENSION || height > MAX_IMAGE_DIMENSION) {
        float ratio = calculateScaleRatio(width, height);
        Log.i("Photo Editor", "Scaled Bitmap: " + ratio);
        srcBitmap = readRoughScaledBitmap(is, ratio);
        ratio = calculateScaleRatio(srcBitmap.getWidth(), srcBitmap.getHeight());
        srcBitmap = scaleBitmap(srcBitmap, ratio);
    } else {
        Log.i("Photo Editor", "NOT Scaled Bitmap ");
        srcBitmap = BitmapFactory.decodeStream(is);
    }
    is.close();
    return srcBitmap;
}

From source file:Main.java

public static byte[] bitmapFromUriToByteArray(Context context, Uri photoUri) throws IOException {
    Bitmap srcBitmap = bitmapFromUri(context, photoUri);
    String type = context.getContentResolver().getType(photoUri);
    byte[] bMapArray = bitmapToByteArray(srcBitmap, type);
    return bMapArray;
}

From source file:Main.java

/**
 * Convert image uri to file//from w  w w.  j  ava  2 s  .c o m
 */
public static String/*File*/ convertImageUriToFile(Context context, Uri imageUri) {
    Cursor cursor = null;
    try {
        String[] projection = { MediaStore.Images.Media.DATA,
                MediaStore.Images.Media._ID /*, MediaStore.Images.ImageColumns.ORIENTATION*/ };
        cursor = context.getContentResolver().query(imageUri, projection, // Which columns to return
                null, // WHERE clause; which rows to return (all rows)
                null, // WHERE clause selection arguments (none)
                null); // Order-by clause (ascending by name)

        int file_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        //int orientation_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.ORIENTATION);

        if (cursor.moveToFirst()) {
            //String orientation = cursor.getString(orientation_ColumnIndex);
            return cursor.getString(file_ColumnIndex)/*new File(cursor.getString(file_ColumnIndex))*/;
        }
        return null;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:Main.java

/**
 * Get the value of the data column for this Uri. This is useful for
 * MediaStore Uris, and other file-based ContentProviders.
 *
 * @param context       The context.//from w w w.j a va  2s.co  m
 * @param uri           The Uri to query.
 * @param selection     (Optional) Filter used in the query.
 * @param selectionArgs (Optional) Selection arguments used in the query.
 * @return The value of the _data column, which is typically a file path.
 */
public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {

    final String[] projection = { MediaStore.MediaColumns.DATA };
    final Cursor cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);

    if (cursor != null) {
        try {

            if (cursor.moveToFirst()) {
                return cursor.getString(0);
            }
        } finally {
            cursor.close();
        }
    }
    return null;
}

From source file:com.mikecorrigan.bohrium.pubsub.PubSubClient.java

private static String getDevId(final Context context) {
    if (context == null) {
        Log.e(TAG, "getDevId: invalid context");
        return null;
    }//from  w  w w . j ava 2s  . c  o m

    return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
}

From source file:Main.java

public static Bitmap getTempBitmap(Context context) {
    Bitmap capturedBitmap = null;//  w  w w.  j a  va  2s .  co  m
    final File file = getTempFile(context);
    try {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 4;

        AssetFileDescriptor fileDescriptor = context.getContentResolver()
                .openAssetFileDescriptor(Uri.fromFile(file), "r");
        capturedBitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor.getFileDescriptor(), null, options);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return capturedBitmap;
}

From source file:Main.java

public static String getPath(Context context, Uri uri) {
    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor;/* www.j  av a 2  s  .co 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;
}