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:com.trk.aboutme.facebook.Settings.java

/**
 * Acquire the current attribution id from the facebook app.
 * @return returns null if the facebook app is not present on the phone.
 *//* w w  w  .jav  a2s  .co m*/
public static String getAttributionId(ContentResolver contentResolver) {
    String[] projection = { ATTRIBUTION_ID_COLUMN_NAME };
    Cursor c = contentResolver.query(ATTRIBUTION_ID_CONTENT_URI, projection, null, null, null);
    if (c == null || !c.moveToFirst()) {
        return null;
    }
    String attributionId = c.getString(c.getColumnIndex(ATTRIBUTION_ID_COLUMN_NAME));
    c.close();
    return attributionId;
}

From source file:com.maskyn.fileeditorpro.util.AccessStorageApi.java

public static String getName(Context context, Uri uri) {

    if (uri == null || uri.equals(Uri.EMPTY))
        return "";

    String fileName = "";
    try {/*from  w  w w  . j av a  2s .  c  o  m*/
        String scheme = uri.getScheme();
        if (scheme.equals("file")) {
            fileName = uri.getLastPathSegment();
        } else if (scheme.equals("content")) {
            String[] proj = { MediaStore.Images.Media.DISPLAY_NAME };
            Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null);
            if (cursor != null && cursor.getCount() != 0) {
                int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME);
                cursor.moveToFirst();
                fileName = cursor.getString(columnIndex);
            }
            if (cursor != null) {
                cursor.close();
            }
        }
    } catch (Exception ex) {
        return "";
    }
    return fileName;
}

From source file:net.peterkuterna.android.apps.devoxxsched.io.RemoteSessionsHandler.java

private static boolean isSessionUpdated(Uri uri, JSONObject session, ContentResolver resolver)
        throws JSONException {
    final Cursor cursor = resolver.query(uri, SessionsQuery.PROJECTION, null, null, null);
    try {//from   ww  w.  j  a  v  a  2  s  .  c  o  m
        if (!cursor.moveToFirst())
            return false;

        final String curTitle = cursor.getString(SessionsQuery.TITLE).toLowerCase().trim();
        final String curSummary = cursor.getString(SessionsQuery.SUMMARY).toLowerCase().trim();
        final String curExperience = cursor.getString(SessionsQuery.EXPERIENCE).toLowerCase().trim();
        final String curType = cursor.getString(SessionsQuery.TYPE).toLowerCase().trim();
        final String newTitle = session.getString("title").toLowerCase().trim();
        final String newSummary = session.getString("summary").toLowerCase().trim();
        final String newExperience = session.getString("experience").toLowerCase().trim();
        final String newType = session.getString("type").toLowerCase().trim();

        return (!curTitle.equals(newTitle) || !curSummary.equals(newSummary)
                || !curExperience.equals(newExperience) || !curType.equals(newType));
    } finally {
        cursor.close();
    }
}

From source file:Main.java

public static File convertImageUriToFile(Uri imageUri, Activity activity) {
    Cursor cursor = null;
    try {/*from  w w w . j  a  va2  s  .  c om*/
        String[] proj = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID,
                MediaStore.Images.ImageColumns.ORIENTATION };
        cursor = activity.managedQuery(imageUri, proj, null, null, null);
        int file_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        int orientation_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.ORIENTATION);
        if (cursor.moveToFirst()) {
            String orientation = cursor.getString(orientation_ColumnIndex);
            return new File(cursor.getString(file_ColumnIndex));
        }
        return null;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:Main.java

static String convertUriToPath(Context context, Uri uri) {
    Log.v(TAG, "convertUriToPath : " + uri + " @" + context);

    String path = null;//w  ww  .  j  a  v a 2s  .c  o  m
    if (null != uri) {
        String scheme = uri.getScheme();
        if (null == scheme || scheme.equals("") || scheme.equals(ContentResolver.SCHEME_FILE)) {
            path = uri.getPath();
        } else if (scheme.equals("http")) {
            path = uri.toString();
        } else if (scheme.equals(ContentResolver.SCHEME_CONTENT)) {
            String[] projection = new String[] { MediaStore.MediaColumns.DATA };
            Cursor cursor = null;
            try {
                cursor = context.getContentResolver().query(uri, projection, null, null, null);
                if (null == cursor || 0 == cursor.getCount() || !cursor.moveToFirst()) {
                    throw new IllegalArgumentException("Given Uri could not be found in media store");
                }
                int pathIndex = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
                path = cursor.getString(pathIndex);
            } catch (SQLiteException e) {
                throw new IllegalArgumentException(
                        "Given Uri is not formatted in a way so that it can be found in media store.");
            } finally {
                if (null != cursor) {
                    cursor.close();
                }
            }
        } else {
            throw new IllegalArgumentException("Given Uri scheme is not supported");
        }
    }

    Log.v(TAG, "convertUriToPath : >" + path);
    return path;
}

From source file:ee.ioc.phon.android.speak.Utils.java

/**
 * TODO: should we immediately return null if id = 0?
 *//*from w w  w.  j  av a 2  s .c o m*/
public static String idToValue(Context context, Uri contentUri, String columnId, String columnUrl, long id) {
    String value = null;
    Cursor c = context.getContentResolver().query(contentUri, new String[] { columnUrl }, columnId + "= ?",
            new String[] { String.valueOf(id) }, null);

    if (c.moveToFirst()) {
        value = c.getString(0);
    }
    c.close();
    return value;
}

From source file:Main.java

/**
 * Get the value of the data column for this Uri. This is useful for
 * MediaStore Uris, and other file-based ContentProviders.
 *
 * @param context/*from   w  w  w .  j a va2 s  . c o  m*/
 *            The context.
 * @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.
 * @author paulburke
 */
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);
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

From source file:com.cyanogenmod.filemanager.util.MediaHelper.java

/**
 * Method that returns an array with all the unique albums paths and ids.
 *
 * @param cr The ContentResolver/*from   w ww  .ja  va2  s.co  m*/
 * @return The albums map
 */
public static Map<String, Long> getAllAlbums(ContentResolver cr) {
    final Map<String, Long> albums = new HashMap<>();
    final String[] projection = { "distinct " + MediaStore.Audio.Media.ALBUM_ID,
            "substr(" + MediaStore.Audio.Media.DATA + ", 0, length(" + MediaStore.Audio.Media.DATA
                    + ") - length(" + MediaStore.Audio.Media.DISPLAY_NAME + "))" };
    final String where = MediaStore.Audio.Media.IS_MUSIC + " = ?";
    Cursor c = cr.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, where, new String[] { "1" },
            null);
    if (c != null) {
        try {
            for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
                long albumId = c.getLong(0);
                String albumPath = c.getString(1);
                albums.put(albumPath, albumId);
            }
        } finally {
            IOUtils.closeQuietly(c);
        }
    }
    return albums;
}

From source file:com.google.samples.apps.topeka.persistence.TopekaDatabaseHelper.java

/**
 * Creates a quiz corresponding to the projection provided from a cursor row.
 * Currently only {@link QuizTable#PROJECTION} is supported.
 *
 * @param cursor The Cursor containing the data.
 * @return The created quiz./*from   ww  w  . java 2 s.  c om*/
 */
private static Quiz createQuizDueToType(Cursor cursor) {
    // "magic numbers" based on QuizTable#PROJECTION
    final String type = cursor.getString(2);
    final String question = cursor.getString(3);
    final String answer = cursor.getString(4);
    final String options = cursor.getString(5);
    final int min = cursor.getInt(6);
    final int max = cursor.getInt(7);
    final int step = cursor.getInt(8);
    final boolean solved = getBooleanFromDatabase(cursor.getString(11));

    switch (type) {
    case JsonAttributes.QuizType.ALPHA_PICKER: {
        return new AlphaPickerQuiz(question, answer, solved);
    }
    case JsonAttributes.QuizType.FILL_BLANK: {
        return createFillBlankQuiz(cursor, question, answer, solved);
    }
    case JsonAttributes.QuizType.FILL_TWO_BLANKS: {
        return createFillTwoBlanksQuiz(question, answer, solved);
    }
    case JsonAttributes.QuizType.FOUR_QUARTER: {
        return createFourQuarterQuiz(question, answer, options, solved);
    }
    case JsonAttributes.QuizType.MULTI_SELECT: {
        return createMultiSelectQuiz(question, answer, options, solved);
    }
    case JsonAttributes.QuizType.PICKER: {
        return new PickerQuiz(question, Integer.valueOf(answer), min, max, step, solved);
    }
    case JsonAttributes.QuizType.SINGLE_SELECT:
        //fall-through intended
    case JsonAttributes.QuizType.SINGLE_SELECT_ITEM: {
        return createSelectItemQuiz(question, answer, options, solved);
    }
    case JsonAttributes.QuizType.TOGGLE_TRANSLATE: {
        return createToggleTranslateQuiz(question, answer, options, solved);
    }
    case JsonAttributes.QuizType.TRUE_FALSE: {
        return createTrueFalseQuiz(question, answer, solved);

    }
    default: {
        throw new IllegalArgumentException("Quiz type " + type + " is not supported");
    }
    }
}

From source file:com.google.samples.apps.topeka.persistence.TopekaDatabaseHelper.java

/**
 * Gets a category from the given position of the cursor provided.
 *
 * @param cursor The Cursor containing the data.
 * @param readableDatabase The database that contains the quizzes.
 * @return The found category.//from w  w w  .ja va 2s.  com
 */
private static Category getCategory(Cursor cursor, SQLiteDatabase readableDatabase) {
    // "magic numbers" based on CategoryTable#PROJECTION
    final String id = cursor.getString(0);
    final String name = cursor.getString(1);
    final String themeName = cursor.getString(2);
    final Theme theme = Theme.valueOf(themeName);
    final String isSolved = cursor.getString(3);
    final boolean solved = getBooleanFromDatabase(isSolved);
    final int[] scores = JsonHelper.jsonArrayToIntArray(cursor.getString(4));

    final List<Quiz> quizzes = getQuizzes(id, readableDatabase);
    return new Category(name, id, theme, quizzes, scores, solved);
}