Example usage for android.database Cursor getColumnIndexOrThrow

List of usage examples for android.database Cursor getColumnIndexOrThrow

Introduction

In this page you can find the example usage for android.database Cursor getColumnIndexOrThrow.

Prototype

int getColumnIndexOrThrow(String columnName) throws IllegalArgumentException;

Source Link

Document

Returns the zero-based index for the given column name, or throws IllegalArgumentException if the column doesn't exist.

Usage

From source file:foam.zizim.android.Util.java

public static List<IncidentsData> showIncidents(String by) {

    Cursor cursor;
    String title;//from w ww. j  a va  2s . com
    String description;
    String location;
    String categories;
    String media;

    if (by.equals("All"))
        cursor = BoskoiApplication.mDb.fetchAllIncidents();
    else
        cursor = BoskoiApplication.mDb.fetchIncidentsByCategories(by);

    if (cursor.moveToFirst()) {
        int idIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.INCIDENT_ID);
        int titleIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.INCIDENT_TITLE);
        int dateIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.INCIDENT_DATE);
        int verifiedIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.INCIDENT_VERIFIED);
        int locationIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.INCIDENT_LOC_NAME);

        int descIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.INCIDENT_DESC);

        int categoryIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.INCIDENT_CATEGORIES);

        int mediaIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.INCIDENT_MEDIA);

        int latitudeIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.INCIDENT_LOC_LATITUDE);

        int longitudeIndex = cursor.getColumnIndexOrThrow(BoskoiDatabase.INCIDENT_LOC_LONGITUDE);

        do {

            IncidentsData incidentData = new IncidentsData();
            mOldIncidents.add(incidentData);

            int id = Util.toInt(cursor.getString(idIndex));
            incidentData.setIncidentId(id);

            title = Util.capitalizeString(cursor.getString(titleIndex));
            incidentData.setIncidentTitle(title);

            description = cursor.getString(descIndex);
            incidentData.setIncidentDesc(description);

            categories = cursor.getString(categoryIndex);
            incidentData.setIncidentCategories(categories);

            location = cursor.getString(locationIndex);
            incidentData.setIncidentLocLongitude(location);

            Util.joinString("Date: ", cursor.getString(dateIndex));
            incidentData.setIncidentDate(cursor.getString(dateIndex));

            media = cursor.getString(mediaIndex);
            incidentData.setIncidentMedia(media);

            incidentData.setIncidentVerified(Util.toInt(cursor.getString(verifiedIndex)));

            incidentData.setIncidentLocLatitude(cursor.getString(latitudeIndex));
            incidentData.setIncidentLocLongitude(cursor.getString(longitudeIndex));

        } while (cursor.moveToNext());
    }

    cursor.close();
    return mOldIncidents;

}

From source file:Main.java

@SuppressLint("NewApi")
public static String uriToPath(Context activity, Uri uri) {
    if (null == uri) {
        return null;
    }//from w  w  w . ja  v  a2  s. c  om
    String urlStr = uri.toString();
    if (urlStr.startsWith("file://")) {
        return uri.getPath();
    }
    Cursor cursor = null;
    String idWhere;
    String id;
    String[] columns = { MediaStore.Images.Media.DATA };
    try {
        if (Build.VERSION.SDK_INT == 19 && DocumentsContract.isDocumentUri(activity, uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            id = split[1];
            idWhere = MediaStore.Images.Media._ID + "=?";
            cursor = activity.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
                    idWhere, new String[] { id }, null);
        } else {
            cursor = activity.getContentResolver().query(uri, columns, null, null, null);
        }
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            if (cursor.moveToFirst()) {
                return cursor.getString(column_index);
            }
        }
    } catch (Exception e) {
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return null;
}

From source file:com.gsma.rcs.ri.messaging.geoloc.DisplayGeoloc.java

private static Geoloc getLastGeoloc(Context ctx, ContactId contact) {
    Cursor cursor = null;
    String where = Message.CONTACT + "='" + contact.toString() + "' AND " + Message.MIME_TYPE + "='"
            + Message.MimeType.GEOLOC_MESSAGE + "' AND " + Message.DIRECTION + " = "
            + Direction.INCOMING.toInt();
    try {/*from ww w.ja v  a  2 s  .  c o  m*/
        cursor = ctx.getContentResolver().query(Message.CONTENT_URI, QUERY_PROJECTION, where, null,
                QUERY_SORT_ORDER);
        if (cursor == null) {
            throw new SQLException("Cannot query last geoloc for contact " + contact);
        }
        if (!cursor.moveToNext()) {
            return null;
        }
        String content = cursor.getString(cursor.getColumnIndexOrThrow(Message.CONTENT));
        return new Geoloc(content);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:com.amytech.android.library.views.imagechooser.threads.MediaProcessorThread.java

public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {

    Cursor cursor = null;
    final String column = "_data";
    final String[] projection = { column };

    try {//from w w w.jav  a  2  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:mil.nga.giat.mage.sdk.utils.MediaUtility.java

public static String getFileAbsolutePath(Uri uri, Context c) {
    String fileName = null;// ww  w .j  av a2s  . c  om
    String scheme = uri.getScheme();
    if (scheme.equals("file")) {
        fileName = uri.getPath();
    } else if (scheme.equals("content")) {
        Cursor cursor = null;
        try {
            String[] proj = { MediaStore.Images.Media.DATA };
            cursor = c.getContentResolver().query(uri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } catch (Exception e) {
            Log.e(LOG_NAME, "Error reading content URI", e);
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }
    return fileName;
}

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  a va  2 s  . c  om*/
 * @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;
        }
    }
}

From source file:com.futurologeek.smartcrossing.crop.CropImageActivity.java

public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {

    Cursor cursor = null;
    final String column = "_data";
    final String[] projection = { column };

    try {/*ww  w .j a  va 2  s. c  om*/
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
        if (cursor != null && cursor.moveToFirst()) {
            final int index = cursor.getColumnIndexOrThrow(column);
            return cursor.getString(index);
        }
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}

From source file:com.pheromone.plugins.FileUtils.java

/**
 * Queries the media store to find out what the file path is for the Uri we supply
 *
 * @param contentUri the Uri of the audio/image/video
 * @param ctx the current applicaiton context
 * @return the full path to the file//  www. j  av a2 s .c o  m
 */
protected static String getRealPathFromURI(Uri contentUri, PhonegapActivity ctx) {
    String[] proj = { _DATA };
    Cursor cursor = ctx.managedQuery(contentUri, proj, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(_DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

From source file:com.orm.androrm.field.CharField.java

@Override
public void set(Cursor c, String fieldName) {
    set(c.getString(c.getColumnIndexOrThrow(fieldName)));
}

From source file:mil.nga.giat.mage.sdk.utils.MediaUtility.java

/**
 * Get the value of the data column for this Uri. This is useful for
 * MediaStore Uris, and other file-based ContentProviders.
 *
 * @param context The context.//from w  w w . ja  va  2s. c  o m
 * @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.
 */
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);
        }
    } catch (Exception e) {
        Log.e(LOG_NAME, "Error getting data column", e);
    } finally {
        if (cursor != null)
            cursor.close();
    }
    return null;
}