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

public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
    Cursor cursor = null;
    String column = MediaStore.Images.Media.DATA;
    String[] projection = { column };
    try {/*from  ww w.j  av  a2 s . co  m*/
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
        if (cursor != null && cursor.moveToFirst()) {
            int index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(index);
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

From source file:Main.java

/**
 * Looks up a contacts display name by contact id - if not found, the
 * address (phone number) will be formatted and returned instead.
 *///from   ww  w .  j ava2  s .c om
public static String getPersonName(Context context, String id, String address) {

    // Check for id, if null return the formatting phone number as the name
    if (id == null || "".equals(id.trim())) {
        if (address != null && !"".equals(address.trim())) {
            return PhoneNumberUtils.formatNumber(address);
        } else {
            return null;
        }
    }

    Cursor cursor = context.getContentResolver().query(Uri.withAppendedPath(Contacts.CONTENT_URI, id),
            new String[] { Contacts.DISPLAY_NAME }, null, null, null);

    if (cursor != null) {
        try {
            if (cursor.getCount() > 0) {
                cursor.moveToFirst();
                String name = cursor.getString(0);
                return name;
            }
        } finally {
            cursor.close();
        }
    }

    if (address != null) {
        return PhoneNumberUtils.formatNumber(address);
    }

    return null;
}

From source file:Main.java

private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
    Cursor cursor = null;
    String column = MediaStore.Images.Media.DATA;
    String[] projection = { column };
    try {//from w  w  w .  j a va 2s.  c  o  m
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
        if (cursor != null && cursor.moveToFirst()) {
            int index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(index);
        }
    } finally {
        if (cursor != null && !cursor.isClosed())
            cursor.close();
    }
    return null;
}

From source file:Main.java

protected static String getImagePath(Uri imageUri, Context context) {
    String scheme = imageUri.getScheme();
    if (scheme.equals("file"))
        return imageUri.getPath();
    Cursor cursor = null;
    try {/*from   ww  w  . jav  a  2s .  c  o m*/
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(imageUri, 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

private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
    Cursor cursor = null;
    String column = MediaStore.Images.Media.DATA;
    String[] projection = { column };
    try {//  www. j  a v  a  2 s .c  o m
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
        if (cursor != null && cursor.moveToFirst()) {
            int index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(index);
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

From source file:Main.java

private 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  a2 s  . co  m
        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.alchemiasoft.common.model.Book.java

public static Book oneFrom(@NonNull Cursor c) {
    final Book book = new Book();
    int index;//from w  w  w. j  av a  2  s  .  co  m
    if ((index = c.getColumnIndex(BookDB.Book.SERVER_ID)) > -1) {
        book.mServerId = c.getString(index);
    }
    if ((index = c.getColumnIndex(BookDB.Book._ID)) > -1) {
        book.mId = c.getLong(index);
    }
    if ((index = c.getColumnIndex(BookDB.Book.TITLE)) > -1) {
        book.mTitle = c.getString(index);
    }
    if ((index = c.getColumnIndex(BookDB.Book.AUTHOR)) > -1) {
        book.mAuthor = c.getString(index);
    }
    if ((index = c.getColumnIndex(BookDB.Book.SOURCE)) > -1) {
        book.mSource = c.getString(index);
    }
    if ((index = c.getColumnIndex(BookDB.Book.DESCRIPTION)) > -1) {
        book.mDescription = c.getString(index);
    }
    if ((index = c.getColumnIndex(BookDB.Book.PAGES)) > -1) {
        book.mPages = c.getInt(index);
    }
    if ((index = c.getColumnIndex(BookDB.Book.NOTES)) > -1) {
        book.mNotes = c.getString(index);
    }
    if ((index = c.getColumnIndex(BookDB.Book.OWNED)) > -1) {
        book.mOwned = c.getInt(index) == 1 ? true : false;
    }
    if ((index = c.getColumnIndex(BookDB.Book.UPDATED_AT)) > -1) {
        book.mUpdatedAt = c.getLong(index);
    }
    return book;
}

From source file:Main.java

static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
    Cursor cursor = null;
    final String column = "_data";
    final String[] projection = { column };
    try {/* w  ww.j  ava2s  .  c om*/
        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 "";
}

From source file:com.coinomi.wallet.ExchangeRatesProvider.java

public static String getCurrencyCodeId(@Nonnull final Cursor cursor) {
    return cursor.getString(cursor.getColumnIndexOrThrow(ExchangeRatesProvider.KEY_CURRENCY_ID));
}

From source file:Main.java

/**
 * Using code from this link: http://hmkcode.com/android-display-selected-image-and-its-real-path/
 * This method will return the absolute path Android.net.Uri.
 * NOTE!!! THIS DOES NOT SUPPORT API 10 OR BELOW!!! IF YOU NEED TO WORK WITH THAT, CHECK LINK ABOVE
 * @param context Context//from  w  ww .j  av  a2  s. co m
 * @param uri Uri to check
 * @return String for the absolute path
 */
public static String getAbsolutePath(Context context, android.net.Uri uri) {
    if (Build.VERSION.SDK_INT >= 19) {
        try {
            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);
            }
            cursor.close();
            return filePath;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    } else {
        try {
            String[] proj = { MediaStore.Images.Media.DATA };
            String result = null;

            CursorLoader cursorLoader = new CursorLoader(context, uri, proj, null, null, null);
            Cursor cursor = cursorLoader.loadInBackground();

            if (cursor != null) {
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                result = cursor.getString(column_index);
            }
            return result;

        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}