Example usage for android.database Cursor getColumnIndex

List of usage examples for android.database Cursor getColumnIndex

Introduction

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

Prototype

int getColumnIndex(String columnName);

Source Link

Document

Returns the zero-based index for the given column name, or -1 if the column doesn't exist.

Usage

From source file:Main.java

public static String getFileFromStorage(Context context, Intent data) {
    Uri pickedImage = data.getData();/*from   w  ww.ja v a 2s. c om*/
    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:edu.mit.mobile.android.locast.data.Locatable.java

public static Uri toGeoUri(Cursor c) {
    if (c.isNull(c.getColumnIndex(Columns._LATITUDE)) || c.isNull(c.getColumnIndex(Columns._LONGITUDE))) {
        return null;
    }//www .j a  va2  s. com
    return Uri.parse("geo:" + c.getDouble(c.getColumnIndex(Columns._LATITUDE)) + ","
            + c.getDouble(c.getColumnIndex(Columns._LONGITUDE)));
}

From source file:Main.java

/**
 * get the connection proxy and port// w  w w . ja v a2 s. co m
 * @param context
 * @return
 */
public static String[] getHostAndProxy(Context context) {
    Uri uri = Uri.parse("content://telephony/carriers/preferapn");
    Cursor mCursor = context.getContentResolver().query(uri, null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToNext();
        String proxyStr = mCursor.getString(mCursor.getColumnIndex("proxy"));
        String port = mCursor.getString(mCursor.getColumnIndex("port"));
        return new String[] { proxyStr, port };
    }
    return null;
}

From source file:Main.java

public static String query(Context context, Uri uri) {
    if (context == null || uri == null)
        return "";
    Cursor cursor = context.getContentResolver().query(uri,
            new String[] { MediaStore.Images.ImageColumns.DATA }, null, null, null);
    cursor.moveToNext();// www. j a va2  s .  c  o  m
    return cursor.getString(cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA));
}

From source file:Main.java

public static final String getTelefone(final Context contexto, final String idContato) {
    ContentResolver cr = contexto.getContentResolver();
    Cursor phones = cr.query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + " = " + idContato, null, null);
    String numero = "";
    while (phones.moveToNext()) {
        numero = phones.getString(phones.getColumnIndex(Phone.NUMBER));
        break;/*  w  w  w  .j a v  a  2s  .  c o m*/
    }
    phones.close();
    return numero;
}

From source file:Main.java

static String getAccessTokenFromTable(Context context, String tableName) {
    String token = null;//from   w ww  . j a va  2 s  . c om
    try {
        SQLiteDatabase db = context.openOrCreateDatabase(DEPRECATED_DATABASE_NAME, 0, null);
        Cursor c = db.rawQuery(
                "SELECT " + DEPRECATED_ACCESS_TOKEN_COLUMN + " FROM " + tableName + " WHERE local_id=0", null);
        if (c.moveToFirst() && c.getColumnIndex(DEPRECATED_ACCESS_TOKEN_COLUMN) != -1) {
            token = c.getString(c.getColumnIndex(DEPRECATED_ACCESS_TOKEN_COLUMN));
        }
        c.close();
        db.close();
    } catch (SQLException e) {
        // DB doesn't exist
    }
    return token;
}

From source file:Main.java

@SuppressLint("NewApi")
public static String getRealPathFromURI_API19(Context context, Uri uri) {
    String filePath = "";
    String wholeID = DocumentsContract.getDocumentId(uri);

    // Split at colon, use second item in the array
    String id = wholeID.split(":")[1];

    String[] column = { MediaStore.Images.Media.DATA };

    // where id is equal to
    String sel = MediaStore.Images.Media._ID + "=?";

    Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column,
            sel, new String[] { id }, null);

    int columnIndex = cursor.getColumnIndex(column[0]);

    if (cursor.moveToFirst()) {
        filePath = cursor.getString(columnIndex);
    }/*from w w  w  .  j  a  v a2s.com*/
    cursor.close();
    return filePath;
}

From source file:Main.java

public static int getRotationFromUri(Context context, Uri imageUri) {
    String[] orientationColumn = { MediaStore.Images.Media.ORIENTATION };
    Cursor cur = context.getContentResolver().query(imageUri, orientationColumn, null, null, null);
    int orientation = -1;
    if (cur != null && cur.moveToFirst()) {
        orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
    }//from   ww w.ja va2 s  .c  o m

    return orientation;
}

From source file:Main.java

public static String getRecentCallsInfo(Context context) {
    StringBuilder stringBuilder = new StringBuilder();
    Cursor cursor = context.getContentResolver().query(Calls.CONTENT_URI, null, null, null,
            Calls.DATE + " DESC");
    int number = cursor.getColumnIndex(Calls.NUMBER);
    int name = cursor.getColumnIndex(Calls.CACHED_NAME);
    int type = cursor.getColumnIndex(Calls.TYPE);
    int date = cursor.getColumnIndex(Calls.DATE);
    int duration = cursor.getColumnIndex(Calls.DURATION);
    while (cursor.moveToNext()) {
        String phNumber = cursor.getString(number);
        String cachedName = cursor.getString(name);
        String callType = cursor.getString(type);
        String callDuration = cursor.getString(duration);

        String callDate = cursor.getString(date);
        Date callDayTime = new Date(Long.valueOf(callDate));

        String dir;/*from   w  w w.  j a v  a  2s. co  m*/
        int dircode = Integer.parseInt(callType);
        switch (dircode) {
        case Calls.OUTGOING_TYPE:
            dir = "OUTGOING";
            break;
        case Calls.INCOMING_TYPE:
            dir = "INCOMING";
            break;
        case Calls.MISSED_TYPE:
            dir = "MISSED";
            break;
        default:
            dir = "UNKNOWN " + dircode;
        }

        stringBuilder.append("\nPhone Number:--- ").append(phNumber).append("\nPhone formatted:--- ")
                .append(PhoneNumberUtils.formatNumber(phNumber)).append("\nCached name:--- ").append(cachedName)
                .append("\nCall Type:--- ").append(dir).append("\nCall Date:--- ").append(callDayTime)
                .append("\nCall duration in sec :--- ").append(callDuration)
                .append("\n----------------------------------");
    }
    cursor.close();
    return stringBuilder.toString();
}

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

/**
 * Get the latitude/longitude from the row currently selected in the cursor. Requires Locatable.Columns._LATITUDE and Locatable.Columns._LONGITUDE to be selected.
 * @param c/*from w  w w.  j  a  va  2 s.co  m*/
 * @return
 */
public static Location toLocation(Cursor c) {
    final int lat_idx = c.getColumnIndex(Columns._LATITUDE);
    final int lon_idx = c.getColumnIndex(Columns._LONGITUDE);
    if (c.isNull(lat_idx) || c.isNull(lon_idx)) {
        return null;
    }
    final Location l = new Location("internal");
    l.setLatitude(c.getDouble(lat_idx));
    l.setLongitude(c.getDouble(lon_idx));
    return l;
}