Example usage for android.database Cursor getColumnIndex

List of usage examples for android.database Cursor getColumnIndex

Introduction

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

Prototype

int getColumnIndex(String columnName);

Source Link

Document

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

Usage

From source file:Main.java

public static Uri getAudioContentUri(Context context, File file) {
    String filePath = file.getAbsolutePath();
    Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            new String[] { MediaStore.Audio.Media._ID }, MediaStore.Audio.Media.DATA + "=?",
            new String[] { filePath }, null);
    cursor.moveToFirst();//from  w  ww  .j  a  v  a  2 s  .  c om
    int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));
    Uri baseUri = Uri.parse("content://media/external/audio/media");
    return Uri.withAppendedPath(baseUri, "" + id);
}

From source file:Main.java

/**
 * Try to return the absolute file path from the given Uri
 *
 * @param context//from   ww  w . j av a 2  s  .  c o m
 * @param uri
 * @return the file path or null
 */
public static String uri2FilePath(final Context context, final Uri uri) {
    if (null == uri)
        return null;
    final String scheme = uri.getScheme();
    String data = null;
    if (scheme == null)
        data = uri.getPath();
    else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
        data = uri.getPath();
    } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
        Cursor cursor = context.getContentResolver().query(uri,
                new String[] { MediaStore.Images.ImageColumns.DATA }, null, null, null);
        if (null != cursor) {
            if (cursor.moveToFirst()) {
                int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
                if (index > -1) {
                    data = cursor.getString(index);
                }
            }
            cursor.close();
        }
    }
    return data;
}

From source file:cn.edu.wyu.documentviewer.model.DocumentInfo.java

public static String getCursorString(Cursor cursor, String columnName) {
    final int index = cursor.getColumnIndex(columnName);
    return (index != -1) ? cursor.getString(index) : null;
}

From source file:cn.edu.wyu.documentviewer.model.DocumentInfo.java

/**
 * Missing or null values are returned as 0.
 *//* www  .  ja v a  2s  .c  o m*/
public static int getCursorInt(Cursor cursor, String columnName) {
    final int index = cursor.getColumnIndex(columnName);
    return (index != -1) ? cursor.getInt(index) : 0;
}

From source file:cn.edu.wyu.documentviewer.model.DocumentInfo.java

/**
 * Missing or null values are returned as -1.
 *///w ww . j  a va  2  s .c  o m
public static long getCursorLong(Cursor cursor, String columnName) {
    final int index = cursor.getColumnIndex(columnName);
    if (index == -1)
        return -1;
    final String value = cursor.getString(index);
    if (value == null)
        return -1;
    try {
        return Long.parseLong(value);
    } catch (NumberFormatException e) {
        return -1;
    }
}

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  a va  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

public static String getThumbnailPathFromURI(Uri contentUri, ContentResolver content_resolver) {

    String uri = "";

    Cursor cursor = MediaStore.Images.Thumbnails.queryMiniThumbnail(content_resolver,
            ContentUris.parseId(contentUri), MediaStore.Images.Thumbnails.MINI_KIND, null);

    if (cursor != null && cursor.getCount() > 0) {
        cursor.moveToFirst();//**EDIT**
        uri = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Thumbnails.DATA));
    }/*from ww  w. ja  va 2 s  . c  o  m*/

    return uri;
}

From source file:Main.java

public static String getRealPathFromURI(Context context, Uri contentURI) {
    String TAG = "PINGUINO-getRealPathFromURI";
    String result;/*from   w w  w  .ja va2 s  .  c  om*/
    Cursor cursor = context.getContentResolver().query(contentURI, null, null, null, null);
    if (cursor == null) {
        result = contentURI.getPath();
    } else {
        Log.d(TAG, "cursor1:" + cursor);
        cursor.moveToFirst();
        Log.d(TAG, "cursor2:" + cursor);
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        Log.d(TAG, "idx:" + idx);
        result = cursor.getString(idx);
        Log.d(TAG, "result:" + result);
        cursor.close();
    }
    return result;
}

From source file:com.nextgis.mobile.map.Layer.java

protected static String getFileNameByUri(final Context context, Uri uri, String defaultName) {
    String fileName = defaultName;
    Uri filePathUri = uri;/* ww w .  j av  a2  s.  c  o  m*/
    try {
        if (uri.getScheme().toString().compareTo("content") == 0) {
            Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
            if (cursor.moveToFirst()) {
                int column_index = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
                //Instead of "MediaStore.Images.Media.DATA" can be used "_data"
                filePathUri = Uri.parse(cursor.getString(column_index));
                fileName = filePathUri.getLastPathSegment().toString();
            }
        } else if (uri.getScheme().compareTo("file") == 0) {
            fileName = filePathUri.getLastPathSegment().toString();
        } else {
            fileName = fileName + "_" + filePathUri.getLastPathSegment();
        }
    } catch (Exception e) {
        //do nothing, only return default file name;
        Log.d(TAG, e.getLocalizedMessage());
    }
    return fileName;
}

From source file:Main.java

private static Bitmap rotateMediaImage(ContentResolver contentResolver, Uri uri, Bitmap image)
        throws FileNotFoundException {

    Cursor c = contentResolver.query(uri, null, null, null, null);
    try {//from w  ww.  ja  va2  s  .c  o m
        if (c != null) {
            if (c.moveToFirst()) {

                int index = c.getColumnIndex(ImageColumns.ORIENTATION);
                int degrees = index < 0 ? 0 : c.getInt(index);

                if (degrees != 0) {
                    return rotate(image, degrees);
                }
                return image;
            }
        }
    } finally {
        if (c != null) {
            c.close();
        }
    }
    return image;
}