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

/**
 * Copy a file from an Uri.//  w  w  w  .  j av  a  2s.  c om
 * @param ctx Context.
 * @param src Source file.
 * @param dst Destination file.
 * @return {@code true} if the copy succeeded, {@code false} otherwise.
 */
public static boolean copyFile(Context ctx, Uri srcUri, File dst) {
    boolean retVal = false;
    try {
        InputStream inStream = ctx.getContentResolver().openInputStream(srcUri);
        try {
            OutputStream outStream = new FileOutputStream(dst);
            try {
                copyFile(inStream, outStream);
                retVal = true;
            } finally {
                outStream.close();
            }
        } finally {
            inStream.close();
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return retVal;
}

From source file:Main.java

public static int getStatus(Context context) {
    Cursor query = null;/*from  w  ww.  ja  va  2s  .  com*/
    String packageName = context.getPackageName();
    try {
        query = context.getContentResolver().query(Uri.parse("content://com.lbe.security.miui.permmgr/active"),
                null, "pkgName=?", new String[] { packageName }, null);
        if (query == null) {
            return 0;
        }
        if (query.moveToFirst()) {
            int status = query.getInt(query.getColumnIndex("userAccept"));
            if (query == null) {
                return status;
            }
        }
        query.close();
    } catch (Exception e) {

        return -1;
    }

    return -1;
}

From source file:sg.macbuntu.android.pushcontacts.SmsReceiver.java

private static String getNameFromPhoneNumber(Context context, String phone) {
    Cursor cursor = context.getContentResolver().query(
            Uri.withAppendedPath(Contacts.Phones.CONTENT_FILTER_URL, phone),
            new String[] { Contacts.Phones.NAME }, null, null, null);
    if (cursor != null) {
        try {// www. ja  v a  2s.  com
            if (cursor.getCount() > 0) {
                cursor.moveToFirst();
                String name = cursor.getString(0);
                Log.e("PUSH_CONTACTS", "Pushed name: " + name);
                return name;
            }
        } finally {
            cursor.close();
        }
    }
    return null;
}

From source file:Main.java

/**
 * @see android.provider.Settings.Global#DEVICE_PROVISIONED
 *///from  www .j  a  v  a 2 s .c om
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static boolean isDeviceProvisioned(Context context) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1)
        return true;
    if (context == null)
        return true;
    if (context.getContentResolver() == null)
        return true;
    return Settings.Global.getInt(context.getContentResolver(), Settings.Global.DEVICE_PROVISIONED, 0) != 0;
}

From source file:Main.java

/**
 * Calculate the dimension of the status bar
 *
 * @param context The current context//ww  w. java  2s.co  m
 * @return The height of the status bar
 */
public static int calculateStatusBarHeight(Context context) {
    // CyanogenMod specific featured (DO NOT RELAY IN INTERNAL VARS)
    boolean hiddenStatusBar = Settings.System.getInt(context.getContentResolver(), "expanded_desktop_state",
            0) == 1 && Settings.System.getInt(context.getContentResolver(), "expanded_desktop_style", 0) == 2;
    int result = 0;

    // On kitkat we can use the translucent bars to fill all the screen
    if (!isKitKat() && !hiddenStatusBar && !(context instanceof Activity)) {
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
    }
    return result;
}

From source file:Main.java

public static String getFileFromStorage(Context context, Intent data) {
    Uri pickedImage = data.getData();/*from ww  w.  j a v  a 2 s. co  m*/
    String[] filePath = { MediaStore.Images.Media.DATA };
    String path = "";
    Cursor cursor = context.getContentResolver().query(pickedImage, filePath, null, null, null);
    if (cursor != null) {
        cursor.moveToFirst();
        path = cursor.getString(cursor.getColumnIndex(filePath[0]));
        cursor.close();
    }
    return path;
}

From source file:Main.java

public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    String result = null;/*from w w w .ja  va 2 s.com*/

    try {
        Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        result = cursor.getString(column_index);

    } catch (Exception e) {
        result = null;
    }
    return result;
}

From source file:Main.java

/**
 * Copy a uri file to indicated file path
 * //from  ww w  .jav a2  s. c o m
 * @param context   the application context
 * @param uri       the uri file to copy
 * @param dest      the dest file 
 * @throws IOException
 */
public static void copyUriTo(Context context, Uri uri, File dest) throws IOException {
    InputStream input = null;
    OutputStream output = null;

    try {
        input = context.getContentResolver().openInputStream(uri);
        output = new FileOutputStream(dest);

        copyFileUsingStream(input, output);
    } finally {
        input.close();
        output.close();
    }
}

From source file:Main.java

public static LinkedHashMap<String, Uri> GetMatchedContactsList(Context context, String searchTerm) {
    LinkedHashMap<String, Uri> contactList = new LinkedHashMap<String, Uri>();
    ContentResolver cr = context.getContentResolver();
    String columns[] = { ContactsContract.Contacts.DISPLAY_NAME,
            ContactsContract.Contacts.PHOTO_THUMBNAIL_URI };
    Cursor cur;/*from w w w  . ja  v a 2 s. com*/
    if (searchTerm == null) {
        cur = cr.query(ContactsContract.Contacts.CONTENT_URI, columns, null, null, null);
    } else {
        cur = cr.query(ContactsContract.Contacts.CONTENT_URI, columns,
                ContactsContract.Contacts.DISPLAY_NAME + " LIKE "
                        + DatabaseUtils.sqlEscapeString("%" + searchTerm + "%"),
                null, ContactsContract.Contacts.DISPLAY_NAME + " ASC");
    }
    if (cur != null) {
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                String photoURI = cur
                        .getString(cur.getColumnIndex(ContactsContract.Contacts.PHOTO_THUMBNAIL_URI));
                if (photoURI != null) {
                    Uri thumbUri = Uri.parse(photoURI);
                    contactList.put(name, thumbUri);
                }
            }
        }
        cur.close();
    }
    return contactList;
}

From source file:Main.java

/**
 * @param context/*from   w  ww. jav a2s  .c  o m*/
 * @param data
 * @return
 */
@Nullable
public static byte[] retrieveSelectedImage(@NonNull Context context, @NonNull Intent data) {
    InputStream inStream = null;
    Bitmap bitmap = null;
    try {
        inStream = context.getContentResolver().openInputStream(data.getData());
        bitmap = BitmapFactory.decodeStream(inStream);
        final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
        return outStream.toByteArray();
    } catch (FileNotFoundException e) {
        return null;
    } finally {
        if (inStream != null) {
            try {
                inStream.close();
            } catch (IOException ignored) {
            }
        }
        if (bitmap != null) {
            bitmap.recycle();
        }
    }
}