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

/**
 * Get the cursor/*from   w w  w.j  a va  2  s .  co m*/
 * @param cursor Cursor from where read value
 * @param index int to read
 * @return Object read
 */
private static Object getCursorValue(Cursor cursor, int index) {
    switch (cursor.getType(index)) {
    case Cursor.FIELD_TYPE_INTEGER:
        return Integer.valueOf(cursor.getInt(index));
    case Cursor.FIELD_TYPE_FLOAT:
        return Float.valueOf(cursor.getFloat(index));
    case Cursor.FIELD_TYPE_STRING:
        return cursor.getString(index);
    case Cursor.FIELD_TYPE_BLOB:
        return cursor.getBlob(index);
    default:
        return null;
    }
}

From source file:Main.java

private static String[] convertCursorAsStringArrayWithCloseCursor(Cursor cursor, int colIdx) {
    String[] result = null;/*from   w ww  .  ja  v  a2s .  c  o  m*/
    try {
        int resultCount = cursor.getCount();
        if (resultCount > 0) {
            HashSet<String> phones = new HashSet<String>(resultCount);
            while (cursor.moveToNext()) {
                String phone = cursor.getString(0);
                phones.add(phone);
            }
            result = phones.toArray(new String[phones.size()]);
        }
        Log.d(TAG,
                "ConvertCursor As StringArray : found " + resultCount + " String converted from idx " + colIdx);
    } finally {
        cursor.close();
    }
    return result;
}

From source file:Main.java

@SuppressLint("NewApi")
public static String getRealPathFromURI_API19(Context context, Uri uri) {
    String filePath = "";
    if (DocumentsContract.isDocumentUri(context, uri)) {
        String wholeID = DocumentsContract.getDocumentId(uri);
        // Split at colon, use second item in the array
        String[] splits = wholeID.split(":");
        if (splits.length == 2) {
            String id = splits[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);
            }/*  ww  w . j  a  va  2s  .  c  om*/
            cursor.close();
        }
    } else {
        filePath = uri.getPath();
    }
    return filePath;
}

From source file:Main.java

/**
 * @param context The {@link Context} to use.
 * @param id The id of the album./*w  w  w . j a  v  a  2 s  .  com*/
 * @return The release date for an album.
 */
public static final String getReleaseDateForAlbum(final Context context, final long id) {
    if (id == -1) {
        return null;
    }
    Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, id);
    Cursor cursor = context.getContentResolver().query(uri, new String[] { AlbumColumns.FIRST_YEAR }, null,
            null, null);
    String releaseDate = null;
    if (cursor != null) {
        cursor.moveToFirst();
        if (!cursor.isAfterLast()) {
            releaseDate = cursor.getString(0);
        }
        cursor.close();
        cursor = null;
    }
    return releaseDate;
}

From source file:Main.java

/**
 * Handles V19 and up uri's//  w w  w. j av a  2  s.  co m
 * @param context
 * @param contentUri
 * @return path
 */
@TargetApi(Build.VERSION_CODES.KITKAT)
private static String getPathForV19AndUp(Context context, Uri contentUri) {
    String wholeID = DocumentsContract.getDocumentId(contentUri);

    // 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);

    String filePath = "";
    int columnIndex = cursor.getColumnIndex(column[0]);
    if (cursor.moveToFirst()) {
        filePath = cursor.getString(columnIndex);
    }

    cursor.close();
    return filePath;
}

From source file:Main.java

/**
 * @param context The {@link Context} to use.
 * @param id The id of the album.//from  w  ww.j  a  v a2  s .  c om
 * @return The song count for an album.
 */
public static final String getSongCountForAlbum(final Context context, final long id) {
    if (id == -1) {
        return null;
    }
    Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, id);
    Cursor cursor = context.getContentResolver().query(uri, new String[] { AlbumColumns.NUMBER_OF_SONGS }, null,
            null, null);
    String songCount = null;
    if (cursor != null) {
        cursor.moveToFirst();
        if (!cursor.isAfterLast()) {
            songCount = cursor.getString(0);
        }
        cursor.close();
        cursor = null;
    }
    return songCount;
}

From source file:Main.java

public static LinkedList<String> retrieveStringFromCursor(Cursor cursor, String columnName) {
    LinkedList<String> result = new LinkedList<String>();

    if (null == cursor || 0 == cursor.getCount()) {
        return result;
    }/*w  w  w  .  j a v a 2s . co m*/

    try {
        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            String str = cursor.getString(cursor.getColumnIndexOrThrow(columnName));
            result.add(str);
        }
    } catch (Exception e) {
        //do nothing.
    } finally {
        cursor.close();
    }

    return result;
}

From source file:Main.java

public static String getRealPathFromURI(Context context, Uri contentUri) {
    Cursor cursor = null;
    try {/*from   w  w w  .  ja v  a  2  s .c  o m*/
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().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:Main.java

public final static String getRealPathFromURI(Context context, Uri contentUri) {
    Cursor cursor = null;
    try {//from   w  ww . ja  v a  2 s .c  o  m
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().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:Main.java

public static File uri2File(Activity context, Uri uri) {
    File file;//  w w  w .j a  v  a2 s . co m
    String[] project = { MediaStore.Images.Media.DATA };
    Cursor actualImageCursor = context.getContentResolver().query(uri, project, null, null, null);
    if (actualImageCursor != null) {
        int actual_image_column_index = actualImageCursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        actualImageCursor.moveToFirst();
        String img_path = actualImageCursor.getString(actual_image_column_index);
        file = new File(img_path);
    } else {
        file = new File(uri.getPath());
    }
    if (actualImageCursor != null)
        actualImageCursor.close();
    return file;
}