Example usage for android.content ContentResolver query

List of usage examples for android.content ContentResolver query

Introduction

In this page you can find the example usage for android.content ContentResolver query.

Prototype

public final @Nullable Cursor query(@RequiresPermission.Read @NonNull Uri uri, @Nullable String[] projection,
        @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) 

Source Link

Document

Query the given URI, returning a Cursor over the result set.

Usage

From source file:Main.java

public static int getMaxSize(ContentResolver resolver) {
    // Note that this URI is safe to call on the UI thread.
    try (Cursor c = resolver.query(DisplayPhoto.CONTENT_MAX_DIMENSIONS_URI,
            new String[] { DisplayPhoto.DISPLAY_MAX_DIM }, null, null, null)) {
        c.moveToFirst();/*from  w  w  w  .jav a  2 s. c  o  m*/
        return c.getInt(0);
    }
}

From source file:com.appsimobile.appsii.module.HotspotLoader.java

public static CircularArray<HotspotItem> loadHotspots(Context c) {
    ContentResolver r = c.getContentResolver();
    Cursor cursor = r.query(HomeContract.Hotspots.CONTENT_URI, HotspotQuery.PROJECTION, null, null, null);
    if (cursor != null) {
        CircularArray<HotspotItem> result = new CircularArray<>(cursor.getCount());

        try {//  w  w  w .  ja va2 s.  co m
            while (cursor.moveToNext()) {
                long id = cursor.getLong(HotspotQuery.ID);
                float height = cursor.getFloat(HotspotQuery.HEIGHT);
                float ypos = cursor.getFloat(HotspotQuery.Y_POSITION);
                boolean left = cursor.getInt(HotspotQuery.LEFT_BORDER) == 1;
                boolean needsConfiguration = cursor.getInt(HotspotQuery.NEEDS_CONFIGURATION) == 1;
                long defaultPageId = cursor.isNull(HotspotQuery._DEFAULT_PAGE) ? -1L
                        : cursor.getLong(HotspotQuery._DEFAULT_PAGE);

                String name = cursor.getString(HotspotQuery.NAME);

                HotspotItem conf = new HotspotItem();
                conf.init(id, name, height, ypos, left, needsConfiguration, defaultPageId);

                result.addLast(conf);
            }
        } finally {
            cursor.close();
        }
        return result;
    }
    return CollectionUtils.emptyArray();
}

From source file:Main.java

public static String checkNull(Context context, int lastImageId, File fileCapture) {
    final String[] imageColumns = { Images.Media._ID, Images.Media.DATA };
    final String imageOrderBy = Images.Media._ID + " DESC";
    final String imageWhere = Images.Media._ID + ">?";
    final String[] imageArguments = { Integer.toString(lastImageId) };

    ContentResolver contentResolver = context.getContentResolver();
    Cursor cursor = contentResolver.query(Images.Media.EXTERNAL_CONTENT_URI, imageColumns, imageWhere,
            imageArguments, imageOrderBy);
    if (cursor == null)
        return null;

    String newpath = null;/* w  ww .ja  v a  2 s .c o m*/
    if (cursor.getCount() >= 2) {
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            int id = cursor.getInt(cursor.getColumnIndex(Images.Media._ID));
            String data = cursor.getString(cursor.getColumnIndex(Images.Media.DATA));
            if (data.equals(fileCapture.getPath())) {
                int rows = contentResolver.delete(Images.Media.EXTERNAL_CONTENT_URI, Images.Media._ID + "=?",
                        new String[] { Long.toString(id) });
                boolean ok = fileCapture.delete();

            } else {
                newpath = data;
            }
        }
    } else {
        newpath = fileCapture.getPath();
        Log.e("MediaUtils", "Not found duplicate.");
    }

    cursor.close();
    return newpath;
}

From source file:net.niyonkuru.koodroid.html.SubscribersHandler.java

private static boolean subscriberExists(Uri uri, ContentResolver resolver) {
    Cursor cursor = resolver.query(uri, null, null, null, null);
    try {/* ww  w. j av  a 2s . com*/
        return cursor.moveToFirst();
    } finally {
        cursor.close();
    }
}

From source file:Main.java

/**
 * Get the file path from a Media storage URI.
 *///from   w  w  w .j av  a  2s  . c om
public static String getPathFromURI(ContentResolver contentResolver, Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = contentResolver.query(contentUri, proj, null, null, null);
    if (cursor == null) {
        return null;
    }
    try {
        int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        if (!cursor.moveToFirst()) {
            return null;
        } else {
            return cursor.getString(columnIndex);
        }
    } finally {
        cursor.close();
    }
}

From source file:Main.java

public static String getRealPathFromURI(Uri contentUri, ContentResolver content_resolver) {
    Cursor cursor = null;//from  w w  w  .  j  a v  a  2s. com
    try {
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = content_resolver.query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:fr.mixit.android.io.JSONHandler.java

protected static boolean isRowExisting(Uri uri, String[] projection, ContentResolver resolver) {
    final Cursor cursor = resolver.query(uri, projection, null, null, null);
    try {/*from  w ww .jav a 2s. c om*/
        if (!cursor.moveToFirst())
            return false;
    } finally {
        cursor.close();
    }
    return true;
}

From source file:Main.java

/**
 * Converts the content:// scheme to the file path
 * @param contentResolver Provides access to the content model
 * @param contentUri The URI to be converted using content:// scheme
 * @return The converted file path//from w  ww .  ja va2 s  .  c  o  m
 */
public static String getPathFromContentUri(ContentResolver contentResolver, Uri contentUri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = contentResolver.query(contentUri, projection, null, null, null);

    if (cursor != null && cursor.moveToFirst()) {
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        String filePath = cursor.getString(column_index);
        cursor.close();
        return filePath;
    } else {
        return null;
    }
}

From source file:Main.java

public static boolean isExistShortcut(Activity context, String authorities) {
    boolean isInstallShortcut = false;
    final ContentResolver cr = context.getContentResolver();

    final Uri CONTENT_URI = Uri.parse("content://" + authorities + "/favorites?notify=true");
    Cursor c = cr.query(CONTENT_URI, new String[] { "iconPackage" }, "iconPackage=?",
            new String[] { context.getApplication().getPackageName() }, null);
    if (c != null) {
        if (c.getCount() > 0) {
            isInstallShortcut = true;// w w  w  . java  2  s  . c  o  m
        }
        c.close();
    }
    return isInstallShortcut;
}

From source file:Main.java

public static Cursor getContactProfile(Context context, String number) {
    if (TextUtils.isEmpty(number)) {
        return null;
    }//from w  w w .  ja  va  2s .  c  o m

    ContentResolver cr = context.getContentResolver();
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));

    return cr.query(uri, CONTACT_PROJ, null, null, null);
}