Example usage for android.database Cursor getString

List of usage examples for android.database Cursor getString

Introduction

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

Prototype

String getString(int columnIndex);

Source Link

Document

Returns the value of the requested column as a String.

Usage

From source file:Main.java

/**
 * Reads the data from the {@code column} of the content's {@code queryUri} and returns it as an
 * Array./* w ww. j av a 2 s .c o  m*/
 */
static private Set<String> getColumnContentAsArray(Context context, Uri queryUri, String column) {
    Cursor cursor = context.getContentResolver().query(queryUri, new String[] { column }, null, null, null);
    Set<String> columnValues = new HashSet<>();
    try {
        if (cursor != null && cursor.moveToFirst()) {
            do {
                columnValues.add(cursor.getString(0));
            } while (cursor.moveToNext());
        }
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return columnValues;
}

From source file:Main.java

/**
 * This method to get real path from uri.
 * /*from www  . j  a va2 s.  c  o  m*/
 * @param context
 * @param contentURI
 * @return imagepath
 */
public static String getRealPathFromURI(Context context, Uri contentURI) {
    Cursor cursor = context.getContentResolver().query(contentURI, null, null, null, null);
    if (cursor == null) {
        return contentURI.getPath();
    } else {
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        return cursor.getString(idx);
    }
}

From source file:Main.java

private static ArrayList<String> getNumbers(Context context) {
    ArrayList<String> list = new ArrayList<String>();
    Cursor cursor = null;
    try {/*from  w  w  w .j a v a 2s .  co m*/
        cursor = context.getContentResolver().query(Phone.CONTENT_URI, null, null, null, null);
        int phoneNumberIdx = cursor.getColumnIndex(Phone.NUMBER);
        cursor.moveToFirst();
        do {
            String phoneNumber = cursor.getString(phoneNumberIdx);
            list.add(phoneNumber);
        } while (cursor.moveToNext());
    } catch (Exception e) {
        Log.d(context.getPackageName(), e.toString());
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    return list;
}

From source file:Main.java

/**
 * @param mContext/*  w  ww  . j a  va  2  s . c o  m*/
 * @param number
 * @return sunrise.l String 2012-5-28
 */
public static String getNameFormNumber(Context mContext, String number) {
    String name = null;

    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, number);
    Cursor cur = mContext.getContentResolver().query(uri,
            new String[] { ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);
    if (cur != null && cur.moveToFirst()) {
        int nameIndex = cur.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME);
        name = cur.getString(nameIndex);
    }
    cur.close();
    return name;
}

From source file:Main.java

/**
 * Get path of video from uri/*w  w w  .  j ava 2s . c  o m*/
 *
 * @param contentResolver
 * @param contentURI
 * @return path of video. Null if not found.
 */
public static String getRealVideoPathFromURI(ContentResolver contentResolver, Uri contentURI) {
    Cursor cursor = contentResolver.query(contentURI, null, null, null, null);
    if (cursor == null)
        return contentURI.getPath();
    else {
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(Video.VideoColumns.DATA);
        try {
            return cursor.getString(idx);
        } catch (Exception exception) {
            return null;
        }
    }
}

From source file:Main.java

/**
 * Get path of image from uri/* ww  w  . ja  v  a 2 s.  c om*/
 *
 * @param contentResolver
 * @param contentURI
 * @return path of image. Null if not found.
 */
public static String getRealImagePathFromURI(ContentResolver contentResolver, Uri contentURI) {
    Cursor cursor = contentResolver.query(contentURI, null, null, null, null);
    if (cursor == null)
        return contentURI.getPath();
    else {
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(Images.ImageColumns.DATA);
        try {
            return cursor.getString(idx);
        } catch (Exception exception) {
            return null;
        }
    }
}

From source file:de.schildbach.wallet.data.ExchangeRatesProvider.java

public static ExchangeRate getExchangeRate(final Cursor cursor) {
    final String currencyCode = cursor
            .getString(cursor.getColumnIndexOrThrow(ExchangeRatesProvider.KEY_CURRENCY_CODE));
    final Coin rateCoin = Coin
            .valueOf(cursor.getLong(cursor.getColumnIndexOrThrow(ExchangeRatesProvider.KEY_RATE_COIN)));
    final Fiat rateFiat = Fiat.valueOf(currencyCode,
            cursor.getLong(cursor.getColumnIndexOrThrow(ExchangeRatesProvider.KEY_RATE_FIAT)));
    final String source = cursor.getString(cursor.getColumnIndexOrThrow(ExchangeRatesProvider.KEY_SOURCE));

    return new ExchangeRate(new org.bitcoinj.utils.ExchangeRate(rateCoin, rateFiat), source);
}

From source file:Main.java

/**
 * Get the contact name from a URI.//  www .j ava2 s  . c  om
 *
 * @param context The context.
 * @param contactUri The contact URI.
 *
 * @return The contact name.
 */
public static String getContactName(final Context context, Uri contactUri) {
    String name = null;
    if (contactUri != null) {
        Cursor cursor = null;
        try {
            cursor = context.getContentResolver().query(contactUri,
                    new String[] { ContactsContract.Contacts.DISPLAY_NAME }, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }

        }
    }

    return name;
}

From source file:Main.java

private static Object getCursorValue(Cursor cursor, Field field, String columnName) {
    Class<?> fieldType = field.getType();

    if (fieldType.equals(int.class) || field.equals(Integer.class)) {
        return cursor.getInt(cursor.getColumnIndex(columnName));
    } else if (fieldType.equals(String.class)) {
        return cursor.getString(cursor.getColumnIndex(columnName));
    } else if (fieldType.equals(long.class) || field.equals(Long.class)) {
        return cursor.getLong(cursor.getColumnIndex(columnName));
    } else if (fieldType.equals(float.class) || field.equals(Float.class)) {
        return cursor.getFloat(cursor.getColumnIndex(columnName));
    } else if (fieldType.equals(double.class) || field.equals(Double.class)) {
        return cursor.getDouble(cursor.getColumnIndex(columnName));
    } else if (fieldType.equals(long.class) || field.equals(Long.class)) {
        return cursor.getLong(cursor.getColumnIndex(columnName));
    } else if (fieldType.equals(Date.class)) {
        long time = cursor.getLong(cursor.getColumnIndex(columnName));
        Date date = new Date(time);
        return date;
    }//from  ww  w .  j  a va  2  s.  c o m
    return cursor.getString(cursor.getColumnIndex(columnName));
}

From source file:Main.java

public static String getRealPathFromURI(Uri contentUri, ContentResolver content_resolver) {
    Cursor cursor = null;
    try {/*from w w w. j av a  2s. com*/
        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();
        }
    }
}