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:com.ferdi2005.secondgram.AndroidUtilities.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 {//w w  w  .j  a  v  a  2s .  c  om
        cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
        if (cursor != null && cursor.moveToFirst()) {
            final int column_index = cursor.getColumnIndexOrThrow(column);
            String value = cursor.getString(column_index);
            if (value.startsWith("content://") || !value.startsWith("/") && !value.startsWith("file://")) {
                return null;
            }
            return value;
        }
    } catch (Exception e) {
        FileLog.e(e);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return null;
}

From source file:net.olejon.mdapp.MedicationActivity.java

private void getManufacturer() {
    SQLiteDatabase sqLiteDatabase = new SlDataSQLiteHelper(mContext).getReadableDatabase();

    String[] queryColumns = { SlDataSQLiteHelper.MANUFACTURERS_COLUMN_ID };
    Cursor cursor = sqLiteDatabase.query(SlDataSQLiteHelper.TABLE_MANUFACTURERS, queryColumns,
            SlDataSQLiteHelper.MANUFACTURERS_COLUMN_NAME + " = " + mTools.sqe(medicationManufacturer), null,
            null, null, null);/* w w  w.j a  va  2s  .com*/

    if (cursor.moveToFirst()) {
        long id = cursor.getLong(cursor.getColumnIndexOrThrow(SlDataSQLiteHelper.MANUFACTURERS_COLUMN_ID));

        Intent intent = new Intent(mContext, ManufacturerActivity.class);
        intent.putExtra("id", id);
        startActivity(intent);
    }

    cursor.close();
    sqLiteDatabase.close();
}

From source file:com.sim2dial.dialer.ChatFragment.java

public String getRealPathFromURI(Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    CursorLoader loader = new CursorLoader(getActivity(), contentUri, proj, null, null, null);
    Cursor cursor = loader.loadInBackground();
    if (cursor != null && cursor.moveToFirst()) {
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        String result = cursor.getString(column_index);
        cursor.close();//  ww  w. j a  v a 2 s.  co  m
        return result;
    }
    return null;
}

From source file:com.amazonaws.mobileconnectors.s3.transferutility.TransferDBUtil.java

/**
 * Queries uncompleted partUpload tasks of a multipart upload and constructs
 * a UploadPartRequest for each task. It's used when resuming a multipart
 * upload/*from  ww  w. j a v a 2 s. c  om*/
 *
 * @param mainUploadId The mainUploadId of a multipart upload task
 * @param multipartId The multipartId of a multipart upload task
 * @return A list of UploadPartRequest
 */
public List<UploadPartRequest> getNonCompletedPartRequestsFromDB(int mainUploadId, String multipartId) {
    final ArrayList<UploadPartRequest> list = new ArrayList<UploadPartRequest>();
    Cursor c = null;
    try {
        c = transferDBBase.query(getPartUri(mainUploadId), null, null, null, null);
        while (c.moveToNext()) {
            if (TransferState.PART_COMPLETED.equals(
                    TransferState.getState(c.getString(c.getColumnIndexOrThrow(TransferTable.COLUMN_STATE))))) {
                continue;
            }
            final UploadPartRequest putPartRequest = new UploadPartRequest()
                    .withId(c.getInt(c.getColumnIndexOrThrow(TransferTable.COLUMN_ID)))
                    .withMainUploadId(c.getInt(c.getColumnIndexOrThrow(TransferTable.COLUMN_MAIN_UPLOAD_ID)))
                    .withBucketName(c.getString(c.getColumnIndexOrThrow(TransferTable.COLUMN_BUCKET_NAME)))
                    .withKey(c.getString(c.getColumnIndexOrThrow(TransferTable.COLUMN_KEY)))
                    .withUploadId(multipartId)
                    .withFile(new File(c.getString(c.getColumnIndexOrThrow(TransferTable.COLUMN_FILE))))
                    .withFileOffset(c.getLong(c.getColumnIndexOrThrow(TransferTable.COLUMN_FILE_OFFSET)))
                    .withPartNumber(c.getInt(c.getColumnIndexOrThrow(TransferTable.COLUMN_PART_NUM)))
                    .withPartSize(c.getLong(c.getColumnIndexOrThrow(TransferTable.COLUMN_BYTES_TOTAL)))
                    .withLastPart(1 == c.getInt(c.getColumnIndexOrThrow(TransferTable.COLUMN_IS_LAST_PART)));
            list.add(putPartRequest);
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return list;
}

From source file:com.nearnotes.NoteEdit.java

@SuppressWarnings("deprecation")
private void populateFields() {

    if (mRowId != null) {
        int settingsResult = mDbHelper.fetchSetting();
        if (settingsResult == mRowId) {
            mCheckBox.setChecked(true);//from ww w .j  a va 2  s .c  om
        } else
            mCheckBox.setChecked(false);
        Cursor note = mDbHelper.fetchNote(mRowId);
        getActivity().startManagingCursor(note);
        mTitleText.setText(note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE)));
        mBodyText.setText(note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY)),
                TextView.BufferType.SPANNABLE);
        autoCompView.setAdapter(null);
        autoCompView.setText(note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_LOCATION)));
        autoCompView.setAdapter(new PlacesAutoCompleteAdapter(getActivity(), R.layout.list_item));
        location = note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_LOCATION));
        longitude = note.getDouble(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_LNG));
        latitude = note.getDouble(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_LAT));
        checkString = note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_CHECK));
        mChecklist = Boolean.parseBoolean(checkString);
    } else {
        autoCompView.requestFocus();
        InputMethodManager imm = (InputMethodManager) getActivity()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(autoCompView, InputMethodManager.SHOW_IMPLICIT);
    }
}

From source file:org.xingjitong.ChatFragment.java

public String getRealPathFromURI(Uri contentUri) {
    String[] proj = { MediaStore.Images.Media.DATA };
    CursorLoader loader = new CursorLoader(getActivity(), contentUri, proj, null, null, null);
    Cursor cursor = loader.loadInBackground();
    if (cursor != null && cursor.moveToFirst()) {
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        String result = cursor.getString(column_index);
        cursor.close();/*w w  w .j  a v a  2s  .co  m*/
        return result;
    }
    cursor.close();
    return null;
}

From source file:com.fvd.nimbus.MainActivity.java

String getImagePath() {

    String[] projection = { MediaStore.Images.Thumbnails._ID, // The columns we want
            MediaStore.Images.Thumbnails.IMAGE_ID, MediaStore.Images.Thumbnails.KIND,
            MediaStore.Images.Thumbnails.DATA };
    String selection = MediaStore.Images.Thumbnails.KIND + "=" + // Select only mini's
            MediaStore.Images.Thumbnails.MINI_KIND;

    String sort = MediaStore.Images.Thumbnails._ID + " DESC";

    //At the moment, this is a bit of a hack, as I'm returning ALL images, and just taking the latest one. There is a better way to narrow this down I think with a WHERE clause which is currently the selection variable
    Cursor myCursor = this.managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection,
            selection, null, sort);/*from w  w w.  ja  va 2s .c  om*/

    long imageId = 0l;
    long thumbnailImageId = 0l;
    String thumbnailPath = "";

    try {
        myCursor.moveToFirst();
        imageId = myCursor.getLong(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID));
        thumbnailImageId = myCursor.getLong(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID));
        thumbnailPath = myCursor.getString(myCursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
    } finally {
        myCursor.close();
    }

    //Create new Cursor to obtain the file Path for the large image

    String[] largeFileProjection = { MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATA };

    String largeFileSort = MediaStore.Images.ImageColumns._ID + " DESC";
    myCursor = this.managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, largeFileProjection, null, null,
            largeFileSort);
    String largeImagePath = "";

    try {
        myCursor.moveToFirst();

        largeImagePath = myCursor
                .getString(myCursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.DATA));
    } finally {
        myCursor.close();
    }
    // These are the two URI's you'll be interested in. They give you a handle to the actual images
    Uri uriLargeImage = Uri.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            String.valueOf(imageId));
    Uri uriThumbnailImage = Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
            String.valueOf(thumbnailImageId));

    if (largeImagePath.length() > 0)
        return largeImagePath;
    else if (uriLargeImage != null)
        return uriLargeImage.getPath();
    else if (uriThumbnailImage != null)
        return uriThumbnailImage.getPath();
    else
        return "";

}

From source file:com.cars.manager.utils.imageChooser.threads.MediaProcessorThread.java

@SuppressLint("NewApi")
protected String getAbsoluteImagePathFromUri(Uri imageUri) {
    String[] proj = { MediaColumns.DATA, MediaColumns.DISPLAY_NAME };

    if (imageUri.toString().startsWith("content://com.android.gallery3d.provider")) {
        imageUri = Uri//from   w ww .  j a  va 2  s  .  co  m
                .parse(imageUri.toString().replace("com.android.gallery3d", "com.google.android.gallery3d"));
    }
    Cursor cursor = context.getContentResolver().query(imageUri, proj, null, null, null);
    cursor.moveToFirst();

    String filePath = "";
    String imageUriString = imageUri.toString();
    if (imageUriString.startsWith("content://com.google.android.gallery3d")
            || imageUriString.startsWith("content://com.google.android.apps.photos.content")
            || imageUriString.startsWith("content://com.android.providers.media.documents")) {
        filePath = imageUri.toString();
    } else {
        filePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaColumns.DATA));
    }
    cursor.close();

    return filePath;
}

From source file:mobisocial.socialkit.musubi.Musubi.java

public DbObj objForCursor(Cursor cursor) {
    try {/* ww  w  .  j  a v  a  2  s  .  co  m*/
        long localId = -1;
        String appId = null;
        String type = null;
        String name = null;
        JSONObject json = null;
        long senderId = -1;
        byte[] hash = null;
        long feedId = -1;
        Integer intKey = null;
        long timestamp = -1;
        Long parentId = null;

        try {
            localId = cursor.getLong(cursor.getColumnIndexOrThrow(DbObj.COL_ID));
        } catch (IllegalArgumentException e) {
        }
        try {
            appId = cursor.getString(cursor.getColumnIndexOrThrow(DbObj.COL_APP_ID));
        } catch (IllegalArgumentException e) {
        }
        try {
            type = cursor.getString(cursor.getColumnIndexOrThrow(DbObj.COL_TYPE));
        } catch (IllegalArgumentException e) {
        }
        try {
            name = cursor.getString(cursor.getColumnIndexOrThrow(DbObj.COL_STRING_KEY));
        } catch (IllegalArgumentException e) {
        }
        try {
            json = new JSONObject(cursor.getString(cursor.getColumnIndexOrThrow(DbObj.COL_JSON)));
        } catch (IllegalArgumentException e) {
        }
        try {
            senderId = cursor.getLong(cursor.getColumnIndexOrThrow(DbObj.COL_IDENTITY_ID));
        } catch (IllegalArgumentException e) {
        }
        try {
            hash = cursor.getBlob(cursor.getColumnIndexOrThrow(DbObj.COL_UNIVERSAL_HASH));
        } catch (IllegalArgumentException e) {
        }
        try {
            feedId = cursor.getLong(cursor.getColumnIndexOrThrow(DbObj.COL_FEED_ID));
        } catch (IllegalArgumentException e) {
        }
        try {
            intKey = cursor.getInt(cursor.getColumnIndexOrThrow(DbObj.COL_INT_KEY));
        } catch (IllegalArgumentException e) {
        }
        try {
            timestamp = cursor.getLong(cursor.getColumnIndexOrThrow(DbObj.COL_TIMESTAMP));
        } catch (IllegalArgumentException e) {
        }
        try {
            int pIndex = cursor.getColumnIndexOrThrow(DbObj.COL_PARENT_ID);
            if (!cursor.isNull(pIndex)) {
                parentId = cursor.getLong(pIndex);
            }
        } catch (IllegalArgumentException e) {
        }

        // Don't require raw field.
        final byte[] raw;
        int rawIndex = cursor.getColumnIndex(DbObj.COL_RAW);
        if (rawIndex == -1 || cursor.isNull(rawIndex)) {
            raw = null;
        } else {
            raw = cursor.getBlob(cursor.getColumnIndexOrThrow(DbObj.COL_RAW));
        }
        return new DbObj(this, appId, feedId, parentId, senderId, localId, type, json, raw, intKey, name,
                timestamp, hash);
    } catch (JSONException e) {
        Log.e(TAG, "Couldn't parse obj.", e);
        return null;
    }
}

From source file:com.benlinskey.greekreference.MainActivity.java

/**
 * Finds and selects the lexicon entry corresponding to the specified URI.
 * @param data the URI of the lexicon entry to select
 */// w w  w  . j  a v a 2  s  .  c  om
private void getLexiconEntry(Uri data) {
    ensureModeIsLexiconBrowse();

    // Get data.
    Cursor cursor = getContentResolver().query(data, null, null, null, null);
    cursor.moveToFirst();
    String id;
    try {
        int idIndex = cursor.getColumnIndexOrThrow(LexiconContract._ID);
        id = cursor.getString(idIndex);
    } catch (IllegalArgumentException e) {
        Log.e(TAG, "Failed to retrieve result from database.");
        throw e;
    }

    // Set this item's state to activated on tablets and scroll the list to the item.
    LexiconBrowseListFragment fragment = (LexiconBrowseListFragment) getFragmentManager()
            .findFragmentById(R.id.item_list_container);
    fragment.selectItem(Integer.parseInt(id));
}