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

@SuppressLint("NewApi")
public static String getRealPathFromUriApi11to18(Context context, Uri contentUri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    String result = null;/*from   w  ww  . j ava 2 s. c om*/

    CursorLoader cursorLoader = new CursorLoader(context, contentUri, projection, null, null, null);
    Cursor cursor = cursorLoader.loadInBackground();

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

From source file:Main.java

public static String uriToString(Context context, Uri uri) {
    String scheme = uri.getScheme();
    if (scheme != null) {
        if (scheme.equals("http") || scheme.equals("https")) {
            return uri.toString();
        } else if (scheme.equals("content") || scheme.equals("file")) {
            Cursor cursor = context.getContentResolver().query(uri,
                    new String[] { OpenableColumns.DISPLAY_NAME }, null, null, null);
            if (cursor.moveToNext()) {
                String name = cursor.getString(0);
                cursor.close();//from   w w  w . j a  v a 2s.c o  m
                return name;
            }
            cursor.close();
            return uri.getPath();
        }
    }
    return uri.toString();
}

From source file:Main.java

public static String getMmsAddress(Context context, long messageId) {
    final String[] projection = new String[] { "address", "contact_id", "charset", "type" };
    final String selection = "type=137"; // "type="+ PduHeaders.FROM,

    Uri.Builder builder = MMS_CONTENT_URI.buildUpon();
    builder.appendPath(String.valueOf(messageId)).appendPath("addr");

    Cursor cursor = context.getContentResolver().query(builder.build(), projection, selection, null, null);

    if (cursor != null) {
        try {/*  www  .  j  a  va  2  s .c o m*/
            if (cursor.moveToFirst()) {
                return cursor.getString(0);
            }
        } finally {
            cursor.close();
        }
    }

    return context.getString(android.R.string.unknownName);
}

From source file:Main.java

/**
 * Gets a contact number and then displays the name of contact in the DP
 *
 * @param context       context of the activity from which it was called.
 * @param contactNumber a string representation of the contact number
 * @return returns the number if no contact exist.
 *//* w ww.  ja va  2 s  .c o  m*/
public static String getContactName(Context context, String contactNumber) {
    String displayName = null;
    Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI, Uri.encode(contactNumber));

    Cursor cur = context.getContentResolver().query(uri,
            new String[] { ContactsContract.PhoneLookup.DISPLAY_NAME }, null, null, null);

    if (cur != null && cur.moveToFirst()) {
        displayName = cur.getString(cur.getColumnIndex(ContactsContract.PhoneLookup.DISPLAY_NAME));
    } else {
        displayName = contactNumber;
    }

    if (!cur.isClosed()) {
        cur.close();
    }

    return displayName;
}

From source file:Main.java

public static String uriToFilePath(Activity context, Uri uri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor actualimagecursor = context.managedQuery(uri, proj, null, null, null);
    int actualImageColumnIndex = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    actualimagecursor.moveToFirst();//  w  w  w. j av  a 2 s.co  m
    String imgPath = actualimagecursor.getString(actualImageColumnIndex);
    return imgPath;
}

From source file:Main.java

public static String getSmsText(Context context, String msgId) {
    String result = null;//from   w ww .  j  a  v  a 2 s .c  om
    try {
        Cursor c = context.getContentResolver().query(Uri.parse("content://sms/inbox"),
                new String[] { "body", }, "_id = ?", new String[] { msgId, }, null);
        if (c.moveToFirst()) {
            result = c.getString(0);
        }
        c.close();
    } catch (Throwable t) {
        LOGE("getSmsText: " + t.getMessage());
        t.printStackTrace();
        result = null;
    }
    return result;
}

From source file:Main.java

public static String getPathFromUrl(Activity context, Uri uri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor actualimagecursor = context.managedQuery(uri, proj, null, null, null);
    int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    actualimagecursor.moveToFirst();/*from w  ww  . j av a 2  s  .  c om*/
    String img_path = actualimagecursor.getString(actual_image_column_index);
    return img_path;
}

From source file:edu.mit.mobile.android.livingpostcards.data.Card.java

public static CharSequence getTitle(Context context, Cursor c) {
    CharSequence title = c.getString(c.getColumnIndexOrThrow(Card.COL_TITLE));
    if (title == null || title.length() == 0) {
        title = context.getText(R.string.untitled);
    }/*from  ww  w.  j  a va  2 s. co  m*/
    return title;
}

From source file:net.sf.sprockets.database.Cursors.java

/**
 * Get the String value in the first row and column.
 *
 * @param close true to close the cursor or false to leave it open
 * @return null if the cursor is empty/*ww  w.  ja v a  2 s  . c om*/
 */
public static String firstString(Cursor cursor, boolean close) {
    String s = cursor.moveToFirst() ? cursor.getString(0) : null;
    close(cursor, close);
    return s;
}

From source file:Main.java

public static String getRealPathFromURI(Context context, Uri contentUri) {
    String res = null;//from w w  w.  ja v  a2  s .com
    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
    if (cursor.moveToFirst()) {
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        res = cursor.getString(column_index);
    }
    cursor.close();
    return res;
}