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

private static Uri exportToGallery(Context context, String filename) {
    // Save the name and description of a video in a ContentValues map.
    final ContentValues values = new ContentValues(2);
    values.put(MediaStore.Video.Media.MIME_TYPE, "image/jpeg");
    values.put(MediaStore.Video.Media.DATA, filename);
    // Add a new record (identified by uri)
    final Uri uri = context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    return uri;/*  ww w  .j  a  va2  s  . com*/
}

From source file:Main.java

public static InputStream getInputStream(Context context, Uri uri) throws IOException {
    InputStream inputStream;//from  www  .java 2s  .  com
    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

/** 
 * Get the value of the data column for this Uri. This is useful
 * for MediaStore Uris, and other file-based ContentProviders.
 * /*from w w  w. j ava2  s.co  m*/
 * @param context The context. 
 * @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.
 */
private static String getGhostMySelfieDataColumn(Context context, Uri uri, String selection,
        String[] selectionArgs) {
    // Projection used to query Android GhostMySelfie Content Provider.
    final String[] projection = { MediaStore.Images.Media.DATA };

    //Query and get a cursor to Android GhostMySelfie
    // Content Provider.
    try (Cursor cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null)) {
        // If selfie is present, get the file path of the selfie.
        if (cursor != null && cursor.moveToFirst())
            return cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
    }

    // No selfie present. returns null.
    return null;
}

From source file:Main.java

public static void setAirplaneMode(Context context, boolean newStatus) {
    boolean airplaneModeOn = isAirplaneModeOn(context);

    if (airplaneModeOn && newStatus) {
        return;/*from www  .  j a v a  2s.  c  o m*/
    }
    if (!airplaneModeOn && !newStatus) {
        return;
    }
    if (airplaneModeOn && !newStatus) {
        Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0);
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        intent.putExtra("state", 0);
        context.sendBroadcast(intent);
        return;
    }
    if (!airplaneModeOn && newStatus) {

        Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 1);
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        intent.putExtra("state", 1);
        context.sendBroadcast(intent);
        return;
    }
}

From source file:Main.java

public static boolean insertMediaStore(Context context, File file, String imageName) {
    if (context == null || file == null || !file.exists()) {
        return false;
    }/*  w  w  w  .  ja v  a 2 s. co m*/

    if (TextUtils.isEmpty(imageName)) {
        imageName = SystemClock.currentThreadTimeMillis() + PNG;
    }
    try {

        MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), imageName,
                null);
        // String imagePath =
        // MediaStore.Images.Media.insertImage(getContentResolver(),
        // bitmap, "", "");

        context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        return false;
    }

    return true;

}

From source file:Main.java

public static Uri getImageUrlWithAuthority(Context context, Uri uri) {
    InputStream is = null;//from ww w .  j a  va2 s.  co  m

    try {

        if (uri.getAuthority() == null) {
            return null;
        } else if (uri.getAuthority() != null) {
            try {
                is = context.getContentResolver().openInputStream(uri);
                Bitmap bmp = BitmapFactory.decodeStream(is);

                bitmapResult = bmp;
                return writeToTempImageAndGetPathUri(context, bmp);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

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   ww  w. j  a  v  a 2  s.  c o  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.
 * @author paulburke
 */
public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {

    Cursor cursor = null;
    final String column = "_data";
    final String[] projection = { column };

    try {
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
        if (cursor != null && cursor.moveToFirst()) {

            final int column_index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(column_index);
        }
    } catch (Exception e) {
        return null;
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

From source file:Main.java

public static String getFirstCNLetterByContactId(Context context, long contactId) {
    String result = "";
    String[] projection = {/*  w w w.j a  v a2  s . c o m*/
            // Data._ID,
            // Data.DISPLAY_NAME,
            // Data.CONTACT_ID,
            Data.DATA12, };
    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 {
            if (cursor.getCount() > 0) {
                while (cursor.moveToNext()) {
                    String nameLetters = cursor.getString(cursor.getColumnIndexOrThrow("data12"));
                    if (null != nameLetters && !"".equals(nameLetters)) {
                        result = nameLetters.substring(0, 1).toUpperCase();
                        break;
                    }
                }
            }

        } finally {
            cursor.close();
        }
    }

    return result;
}

From source file:Main.java

public static String getMmsAddress(Context context, long messageId) {
    final String[] projection = new String[] { "address", "contact_id", "charset", "type" };
    final String selection = "type=137"; // "type="+ PduHeaders.FROM,

    Uri.Builder builder = MMS_CONTENT_URI.buildUpon();
    builder.appendPath(String.valueOf(messageId)).appendPath("addr");

    Cursor cursor = context.getContentResolver().query(builder.build(), projection, selection, null, null);

    if (cursor != null) {
        try {/* ww w.ja v  a2s .co  m*/
            if (cursor.moveToFirst()) {
                return cursor.getString(0);
            }
        } finally {
            cursor.close();
        }
    }

    return context.getString(android.R.string.unknownName);
}

From source file:Main.java

/**
 * method used to lookup the id of a contact photo id
 *
 * @param context//from  w  ww.ja  va2s .c o  m
 *            a context object used to get a content resolver
 * @param phoneNumber
 *            the phone number of the contact
 *
 * @return the id of the contact
 */
public static long lookupPhotoId(Context context, String phoneNumber) {

    long mPhotoId = -1;

    Uri mLookupUri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(phoneNumber));

    String[] mProjection = new String[2];
    mProjection[0] = PhoneLookup._ID;
    mProjection[1] = PhoneLookup.PHOTO_ID;

    Cursor mCursor = context.getContentResolver().query(mLookupUri, mProjection, null, null, null);

    if (mCursor.getCount() > 0) {
        mCursor.moveToFirst();

        mPhotoId = mCursor.getLong(mCursor.getColumnIndex(PhoneLookup._ID));

        mCursor.close();
    }

    return mPhotoId;
}