Example usage for android.database Cursor moveToFirst

List of usage examples for android.database Cursor moveToFirst

Introduction

In this page you can find the example usage for android.database Cursor moveToFirst.

Prototype

boolean moveToFirst();

Source Link

Document

Move the cursor to the first row.

Usage

From source file:com.futurologeek.smartcrossing.crop.CropImageActivity.java

public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {

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

    try {/*from   ww  w  .j a  v  a  2s. c om*/
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
        if (cursor != null && cursor.moveToFirst()) {
            final int index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(index);
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

From source file:Main.java

@SuppressLint("NewApi")
public static String uriToPath(Context activity, Uri uri) {
    if (null == uri) {
        return null;
    }/*from   ww w  . ja  va 2  s. c  o m*/
    String urlStr = uri.toString();
    if (urlStr.startsWith("file://")) {
        return uri.getPath();
    }
    Cursor cursor = null;
    String idWhere;
    String id;
    String[] columns = { MediaStore.Images.Media.DATA };
    try {
        if (Build.VERSION.SDK_INT == 19 && DocumentsContract.isDocumentUri(activity, uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            id = split[1];
            idWhere = MediaStore.Images.Media._ID + "=?";
            cursor = activity.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
                    idWhere, new String[] { id }, null);
        } else {
            cursor = activity.getContentResolver().query(uri, columns, null, null, null);
        }
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            if (cursor.moveToFirst()) {
                return cursor.getString(column_index);
            }
        }
    } catch (Exception e) {
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return null;
}

From source file:com.haoqee.chatsdk.net.Utility.java

/**
 * Get a HttpClient object which is setting correctly .
 * /* ww  w  .j  ava 2s  .co m*/
 * @param context
 *            : context of activity
 * @return HttpClient: HttpClient object
 */
public static DefaultHttpClient getHttpClient(Context context) {
    BasicHttpParams httpParameters = new BasicHttpParams();
    // Set the default socket timeout (SO_TIMEOUT) // in
    // milliseconds which is the timeout for waiting for data.
    HttpConnectionParams.setConnectionTimeout(httpParameters, Utility.SET_CONNECTION_TIMEOUT);
    HttpConnectionParams.setSoTimeout(httpParameters, Utility.SET_SOCKET_TIMEOUT);
    DefaultHttpClient client = new DefaultHttpClient(httpParameters);
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    if (!wifiManager.isWifiEnabled()) {
        // ??PN????
        Uri uri = Uri.parse("content://telephony/carriers/preferapn");
        Cursor mCursor = context.getContentResolver().query(uri, null, null, null, null);
        if (mCursor != null && mCursor.moveToFirst()) {
            // ??????
            String proxyStr = mCursor.getString(mCursor.getColumnIndex("proxy"));
            if (proxyStr != null && proxyStr.trim().length() > 0) {
                HttpHost proxy = new HttpHost(proxyStr, 80);
                client.getParams().setParameter(ConnRouteParams.DEFAULT_PROXY, proxy);
            }
            mCursor.close();
        }
    }
    return client;
}

From source file:ro.weednet.contactssync.platform.ContactManager.java

public static long ensureGroupExists(Context context, Account account) {
    final ContentResolver resolver = context.getContentResolver();

    // Lookup the group
    long groupId = 0;
    final Cursor cursor = resolver.query(Groups.CONTENT_URI, new String[] { Groups._ID },
            Groups.ACCOUNT_NAME + "=? AND " + Groups.ACCOUNT_TYPE + "=? AND " + Groups.TITLE + "=?",
            new String[] { account.name, account.type, GROUP_NAME }, null);
    if (cursor != null) {
        try {/* w  w  w .jav  a2s .c  o  m*/
            if (cursor.moveToFirst()) {
                groupId = cursor.getLong(0);
            }
        } finally {
            cursor.close();
        }
    }

    if (groupId == 0) {
        // Group doesn't exist yet, so create it
        final ContentValues contentValues = new ContentValues();
        contentValues.put(Groups.ACCOUNT_NAME, account.name);
        contentValues.put(Groups.ACCOUNT_TYPE, account.type);
        contentValues.put(Groups.TITLE, GROUP_NAME);
        //   contentValues.put(Groups.GROUP_IS_READ_ONLY, true);
        contentValues.put(Groups.GROUP_VISIBLE, true);

        final Uri newGroupUri = resolver.insert(Groups.CONTENT_URI, contentValues);
        groupId = ContentUris.parseId(newGroupUri);
    }
    return groupId;
}

From source file:edu.mit.mobile.android.locast.data.JsonSyncableItem.java

/**
 * Given a public Uri fragment, finds the local item representing it. If there isn't any such item, null is returned.
 *
 * @param context/*  w ww. ja  va2 s. c  om*/
 * @param dirUri the base local URI to search.
 * @param pubUri A public URI fragment that represents the given item. This must match the result from the API.
 * @return a local URI matching the item or null if none were found.
 */
public static Uri getItemByPubIUri(Context context, Uri dirUri, String pubUri) {
    Uri uri = null;
    final ContentResolver cr = context.getContentResolver();

    final String[] selectionArgs = { pubUri };
    final Cursor c = cr.query(dirUri, PUB_URI_PROJECTION, _PUBLIC_URI + "=?", selectionArgs, null);
    if (c.moveToFirst()) {
        uri = ContentUris.withAppendedId(dirUri, c.getLong(c.getColumnIndex(_ID)));
    }

    c.close();
    return uri;
}

From source file:com.android.providers.downloads.DownloadInfo.java

/**
 * Query and return status of requested download.
 *//*from  ww w.  j a v  a2  s  .c om*/
public static int queryDownloadStatus(ContentResolver resolver, long id) {
    final Cursor cursor = resolver.query(
            ContentUris.withAppendedId(Downloads.Impl.ALL_DOWNLOADS_CONTENT_URI, id),
            new String[] { Downloads.Impl.COLUMN_STATUS }, null, null, null);
    try {
        if (cursor.moveToFirst()) {
            return cursor.getInt(0);
        } else {
            // TODO: increase strictness of value returned for unknown
            // downloads; this is safe default for now.
            return Downloads.Impl.STATUS_PENDING;
        }
    } finally {
        cursor.close();
    }
}

From source file:com.ubuntuone.android.files.provider.MetaUtilities.java

public static ArrayList<U1Node> getPhotoNodesFromDirectory(String directoryResourcePath) {
    String[] projection = Nodes.getDefaultProjection();
    String selection = Nodes.NODE_PARENT_PATH + "=? AND " + Nodes.NODE_MIME + "=?";
    String[] selectionArgs = new String[] { directoryResourcePath, "image/jpeg" };

    Cursor c = sResolver.query(Nodes.CONTENT_URI, projection, selection, selectionArgs, null);

    ArrayList<U1Node> photoNodes = null;
    if (c != null) {
        photoNodes = new ArrayList<U1Node>();
        try {/* w  ww.j  av a  2s. c o m*/
            if (c.moveToFirst()) {
                do {
                    String resourcePath;
                    String key;
                    U1NodeKind kind;
                    Boolean isLive = true;
                    String path;
                    String parentPath;
                    String volumePath;
                    Date whenCreated;
                    Date whenChanged;
                    Long generation;
                    Long generationCreated;
                    String contentPath;

                    resourcePath = c.getString(c.getColumnIndex(Nodes.NODE_RESOURCE_PATH));
                    key = c.getString(c.getColumnIndex(Nodes.NODE_KEY));
                    kind = U1NodeKind
                            .valueOf(c.getString(c.getColumnIndex(Nodes.NODE_KIND)).toUpperCase(Locale.US));
                    isLive = c.getInt(c.getColumnIndex(Nodes.NODE_IS_LIVE)) != 0;
                    path = c.getString(c.getColumnIndex(Nodes.NODE_PATH));
                    parentPath = c.getString(c.getColumnIndex(Nodes.NODE_PARENT_PATH));
                    volumePath = c.getString(c.getColumnIndex(Nodes.NODE_VOLUME_PATH));
                    whenCreated = new Date(c.getLong(c.getColumnIndex(Nodes.NODE_WHEN_CREATED)));
                    whenChanged = new Date(c.getLong(c.getColumnIndex(Nodes.NODE_WHEN_CHANGED)));
                    generation = c.getLong(c.getColumnIndex(Nodes.NODE_GENERATION));
                    generationCreated = c.getLong(c.getColumnIndex(Nodes.NODE_GENERATION_CREATED));
                    contentPath = c.getString(c.getColumnIndex(Nodes.NODE_CONTENT_PATH));

                    U1Node node = new U1Node(resourcePath, kind, isLive, path, parentPath, volumePath, key,
                            whenCreated, whenChanged, generation, generationCreated, contentPath);
                    photoNodes.add(node);
                } while (c.moveToNext());
            }
        } finally {
            c.close();
        }
    }
    return photoNodes;
}

From source file:mil.nga.giat.mage.sdk.utils.MediaUtility.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./*w  ww .ja 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.
 */
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) {
        Log.e(LOG_NAME, "Error getting data column", e);
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

From source file:com.almarsoft.GroundhogReader.lib.DBUtils.java

public static long getGroupLastFetchedNumber(String group, Context context) {

    long lastFetched = -1;

    DBHelper dbhelper = new DBHelper(context);
    SQLiteDatabase readdb = dbhelper.getReadableDatabase();

    String fQuery = "SELECT lastFetched FROM subscribed_groups WHERE name=" + esc(group);
    Cursor cur = readdb.rawQuery(fQuery, null);

    if (cur.getCount() > 0) {
        cur.moveToFirst();
        lastFetched = cur.getInt(0);/*from   ww w .j  a v a 2s.c o  m*/
    }

    cur.close();
    readdb.close();
    dbhelper.close();

    return lastFetched;
}

From source file:com.codebutler.farebot.transit.SuicaTransitData.java

/**
 * ?????//  www .  ja v  a2  s .  c om
 * <pre>http:// sourceforge.jp/projects/felicalib/wiki/suica???????</pre>
 * @param lineCode 
 * @param stationCode 
 * @return ????????0????1??????
 */
private static Station getBusStop(int regionCode, int lineCode, int stationCode) {
    int areaCode = (regionCode >> 6);

    try {
        SQLiteDatabase db = FareBotApplication.getInstance().getSuicaDBUtil().openDatabase();
        Cursor cursor = db.query(TABLE_IRUCA_STATIONCODE, COLUMNS_IRUCA_STATIONCODE,
                String.format("%s = ? AND %s = ?", COLUMN_LINECODE, COLUMN_STATIONCODE),
                new String[] { Integer.toHexString(lineCode), Integer.toHexString(stationCode) }, null, null,
                COLUMN_ID);

        if (!cursor.moveToFirst()) {
            return null;
        }

        // FIXME: Figure out a better way to deal with i18n.
        boolean isJa = Locale.getDefault().getLanguage().equals("ja");
        String companyName = cursor
                .getString(cursor.getColumnIndex(isJa ? COLUMN_COMPANYNAME : COLUMN_COMPANYNAME_EN));
        String stationName = cursor
                .getString(cursor.getColumnIndex(isJa ? COLUMN_STATIONNAME : COLUMN_STATIONNAME_EN));
        return new Station(companyName, null, stationName, null, null, null);

    } catch (Exception e) {
        Log.e("SuicaStationProvider", "getBusStop() error", e);
        return null;
    }
}