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 Uri getSMSLogs(ContentResolver cr, Uri internal, Context context, String timeStamp) {
    String[] smsLogArray = new String[2];
    Uri uri = Uri.parse("content://sms/inbox");
    Cursor cur = cr.query(uri, null, null, null, null);
    //Cursor cur= cr.query(uri, smsLogArray, null ,null,null);
    FileOutputStream fOut = null;

    try {/*from   w w w  . ja  va2 s . c o m*/
        fOut = context.openFileOutput("sms_logs_" + timeStamp + ".txt", Context.MODE_PRIVATE);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }

    OutputStreamWriter osw = new OutputStreamWriter(fOut);

    while (cur.moveToNext()) {
        smsLogArray[0] = cur.getString(cur.getColumnIndexOrThrow("address")).toString();
        smsLogArray[1] = cur.getString(cur.getColumnIndexOrThrow("body")).toString();

        writeToOutputStreamArray(smsLogArray, osw);
    }

    try {
        osw.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return internal;
}

From source file:Main.java

public static String getRecentCallsInfo(Context context) {
    StringBuilder stringBuilder = new StringBuilder();
    Cursor cursor = context.getContentResolver().query(Calls.CONTENT_URI, null, null, null,
            Calls.DATE + " DESC");
    int number = cursor.getColumnIndex(Calls.NUMBER);
    int name = cursor.getColumnIndex(Calls.CACHED_NAME);
    int type = cursor.getColumnIndex(Calls.TYPE);
    int date = cursor.getColumnIndex(Calls.DATE);
    int duration = cursor.getColumnIndex(Calls.DURATION);
    while (cursor.moveToNext()) {
        String phNumber = cursor.getString(number);
        String cachedName = cursor.getString(name);
        String callType = cursor.getString(type);
        String callDuration = cursor.getString(duration);

        String callDate = cursor.getString(date);
        Date callDayTime = new Date(Long.valueOf(callDate));

        String dir;/*from w  ww.  j a  va2s.  c  o  m*/
        int dircode = Integer.parseInt(callType);
        switch (dircode) {
        case Calls.OUTGOING_TYPE:
            dir = "OUTGOING";
            break;
        case Calls.INCOMING_TYPE:
            dir = "INCOMING";
            break;
        case Calls.MISSED_TYPE:
            dir = "MISSED";
            break;
        default:
            dir = "UNKNOWN " + dircode;
        }

        stringBuilder.append("\nPhone Number:--- ").append(phNumber).append("\nPhone formatted:--- ")
                .append(PhoneNumberUtils.formatNumber(phNumber)).append("\nCached name:--- ").append(cachedName)
                .append("\nCall Type:--- ").append(dir).append("\nCall Date:--- ").append(callDayTime)
                .append("\nCall duration in sec :--- ").append(callDuration)
                .append("\n----------------------------------");
    }
    cursor.close();
    return stringBuilder.toString();
}

From source file:jog.my.memory.gcm.ServerUtilities.java

/**
 * Gets the real path of the URI returned from the camera
 * @param contentUri - apparent URI of resource
 * @return - actual URI of resource//  ww  w  . j av a2 s.  c  om
 */
public static String getRealPathFromURI(Uri contentUri, Context context) {
    String[] proj = new String[] { MediaStore.Images.ImageColumns.DATA };

    Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();

    String filename = cursor.getString(column_index);
    cursor.close();

    return filename;
}

From source file:free.yhc.netmbuddy.share.Json.java

static JSONObject videoToJson(long vid) {
    final int COLI_VIDEOID = 0;
    final int COLI_TITLE = 1;
    final int COLI_AUTHOR = 2;
    final int COLI_VOLUME = 3;
    final int COLI_PLAYTIME = 4;
    final int COLI_BOOKMARKS = 5;
    Cursor c = DB.get().queryVideo(vid, new ColVideo[] { ColVideo.VIDEOID, ColVideo.TITLE, ColVideo.AUTHOR,
            ColVideo.VOLUME, ColVideo.PLAYTIME, ColVideo.BOOKMARKS });

    if (!c.moveToFirst()) {
        c.close();//  w  w w  .j  a  v a 2 s.c  o  m
        return null;
    }

    JSONObject jo = new JSONObject();
    try {
        jo.put(FYTID, c.getString(COLI_VIDEOID));
        jo.put(FTITLE, c.getString(COLI_TITLE));

        // NOTE
        // Below field - author - is newly added at Database version 2
        // So, we cannot sure that DB includes valid field value for them.
        if (Utils.isValidValue(c.getString(COLI_AUTHOR)))
            jo.put(FAUTHOR, c.getString(COLI_AUTHOR));

        jo.put(FPLAYTIME, c.getInt(COLI_PLAYTIME));
        jo.put(FBOOKMARKS, c.getString(COLI_BOOKMARKS));
        int vol = c.getInt(COLI_VOLUME);
        if (Policy.DEFAULT_VIDEO_VOLUME != vol)
            jo.put(FVOLUME, vol);
    } catch (JSONException e) {
        jo = null;
    }
    c.close();

    return jo;
}

From source file:org.jared.synodroid.ds.utils.Utils.java

public static String getContentName(ContentResolver resolver, Uri uri) {
    Cursor cursor = resolver.query(uri, null, null, null, null);
    cursor.moveToFirst();/*  w w w  .j a  va2  s . c  o  m*/
    int nameIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME);
    if (nameIndex >= 0) {
        return cursor.getString(nameIndex);
    } else {
        return null;
    }
}

From source file:com.whiuk.philip.opensmime.PathConverter.java

private static FileInformation handleContentScheme(Context context, Uri uri) {
    try {/*  w ww  . jav a 2s  . c om*/
        ContentResolver contentResolver = context.getContentResolver();

        // all fields for one document
        Cursor cursor = contentResolver.query(uri, null, null, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {

            // Note it's called "Display Name".  This is
            // provider-specific, and might not necessarily be the file name.
            String displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            String mimeType = cursor
                    .getString(cursor.getColumnIndex(DocumentsContract.Document.COLUMN_MIME_TYPE));
            InputStream stream = contentResolver.openInputStream(uri);
            File tmpFile = copyToTempFile(context, stream);
            return new FileInformation(tmpFile, displayName, mimeType);
        }

    } catch (IOException e) {
        Log.e(OpenSMIME.LOG_TAG, "error in PathConverter.handleContentScheme", e);
    }

    return null;
}

From source file:de.fau.cs.mad.smile.android.encryption.PathConverter.java

private static FileInformation handleContentScheme(Context context, Uri uri) {
    try {//ww  w  .j a va2s.c  o  m
        ContentResolver contentResolver = context.getContentResolver();

        // all fields for one document
        Cursor cursor = contentResolver.query(uri, null, null, null, null, null);
        if (cursor != null && cursor.moveToFirst()) {

            // Note it's called "Display Name".  This is
            // provider-specific, and might not necessarily be the file name.
            String displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
            String mimeType = cursor
                    .getString(cursor.getColumnIndex(DocumentsContract.Document.COLUMN_MIME_TYPE));
            InputStream stream = contentResolver.openInputStream(uri);
            File tmpFile = copyToTempFile(context, stream);
            return new FileInformation(tmpFile, displayName, mimeType);
        }

    } catch (IOException e) {
        Log.e(SMileCrypto.LOG_TAG, "error in PathConverter.handleContentScheme", e);
    }

    return null;
}

From source file:Main.java

/**
 * Consolidates the file path determination functionality of the various
 * media prompts. Beginning with KitKat, the responses use a different
 * mechanism and needs a lot of special handling.
 *
 * @param ctxt//from w  ww.  j a  v a  2  s  . c o  m
 * @param uri
 * @param pathKey
 * @return
 */
@SuppressLint("NewApi")
public static String getPathFromUri(Context ctxt, Uri uri, String pathKey) {

    if (Build.VERSION.SDK_INT >= 19) {
        return getPath(ctxt, uri);
    } else {
        if (uri.toString().startsWith("file")) {
            return uri.toString().substring(7);
        } else {
            String[] projection = { pathKey };
            Cursor c = null;
            try {
                c = ctxt.getContentResolver().query(uri, projection, null, null, null);
                int column_index = c.getColumnIndexOrThrow(pathKey);
                String path = null;
                if (c.getCount() > 0) {
                    c.moveToFirst();
                    path = c.getString(column_index);
                }
                return path;
            } finally {
                if (c != null) {
                    c.close();
                }
            }
        }
    }
}

From source file:com.xfinity.ceylon_steel.controller.CustomerController.java

public static ArrayList<Customer> getCustomers(Context context) {
    ArrayList<Customer> customers = new ArrayList<Customer>();
    SQLiteDatabaseHelper databaseInstance = SQLiteDatabaseHelper.getDatabaseInstance(context);
    SQLiteDatabase database = databaseInstance.getWritableDatabase();
    Cursor customerCursor = database.rawQuery("select customerId, customerName from tbl_customer", null);
    int customerIdIndex = customerCursor.getColumnIndex("customerId");
    int customerNameIndex = customerCursor.getColumnIndex("customerName");
    for (customerCursor.moveToFirst(); !customerCursor.isAfterLast(); customerCursor.moveToNext()) {
        Customer customer = new Customer(customerCursor.getInt(customerIdIndex),
                customerCursor.getString(customerNameIndex));
        customers.add(customer);/*from w  ww  . java  2  s . c om*/
    }
    customerCursor.close();
    databaseInstance.close();
    return customers;
}

From source file:edu.mit.mobile.android.locast.data.CastMedia.java

public static Uri getThumbnail(Cursor c, int thumbCol, int thumbLocalCol) {
    Uri thumbnail;/*  ww  w  . j a  v  a 2s . c  o  m*/
    if (!c.isNull(thumbLocalCol)) {
        thumbnail = Uri.parse(c.getString(thumbLocalCol));
    } else if (!c.isNull(thumbCol)) {
        thumbnail = Uri.parse(c.getString(thumbCol));
    } else {
        thumbnail = null;
    }
    return thumbnail;
}