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 getRealPathFromURI_API19(Context context, Uri uri) {
    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);
    }//from  www.j  a  va2  s.c  o m
    cursor.close();
    return filePath;
}

From source file:Main.java

/**
 * Converts the content:// scheme to the file path
 * @param contentResolver Provides access to the content model
 * @param contentUri The URI to be converted using content:// scheme
 * @return The converted file path/*from   ww w . ja va2  s .c o m*/
 */
public static String getPathFromContentUri(ContentResolver contentResolver, Uri contentUri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = contentResolver.query(contentUri, projection, null, null, null);

    if (cursor != null && cursor.moveToFirst()) {
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        String filePath = cursor.getString(column_index);
        cursor.close();
        return filePath;
    } else {
        return null;
    }
}

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));
    }// w  w  w.j a  va 2  s. c  om

    return uri;
}

From source file:Main.java

public static Object cursorValue(String column, Cursor cr) {
    Object value = false;//from   w  ww. j a v  a2s .  c om
    int index = cr.getColumnIndex(column);
    switch (cr.getType(index)) {
    case Cursor.FIELD_TYPE_NULL:
        value = false;
        break;
    case Cursor.FIELD_TYPE_STRING:
        value = cr.getString(index);
        break;
    case Cursor.FIELD_TYPE_INTEGER:
        value = cr.getInt(index);
        break;
    case Cursor.FIELD_TYPE_FLOAT:
        value = cr.getFloat(index);
        break;
    case Cursor.FIELD_TYPE_BLOB:
        value = cr.getBlob(index);
        break;
    }
    return value;
}

From source file:Main.java

public static String getPath(Context context, Uri uri) {

    if ("content".equalsIgnoreCase(uri.getScheme())) {
        String[] projection = { "_data" };
        Cursor cursor = null;

        try {//from  w  w w  . j  a  v a 2s .c  om
            cursor = context.getContentResolver().query(uri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow("_data");
            if (cursor.moveToFirst()) {
                return cursor.getString(column_index);
            }
        } catch (Exception e) {

        }
    }

    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

From source file:Main.java

/**
 * Get a uri's user-friendly display name
 * //from   w  w  w  .j a v  a2  s  .  c  o m
 * @param context the application context
 * @param uri     the uri to query
 * 
 * @return a user-friendly display name
 */
public static String getUriDisplayName(Context context, Uri uri) {
    String displayName = null;

    String scheme = uri.getScheme();

    if (scheme.startsWith("content")) {
        String[] proj = { OpenableColumns.DISPLAY_NAME };
        Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null);

        if (cursor != null) {
            int columnIndex = cursor.getColumnIndexOrThrow(OpenableColumns.DISPLAY_NAME);
            cursor.moveToFirst();
            displayName = cursor.getString(columnIndex);

            cursor.close();
        }
    } else if (scheme.startsWith("file")) {
        displayName = uri.getLastPathSegment();
    }

    return displayName;
}

From source file:Main.java

@SuppressLint("NewApi")
public static String getRealPathFromUriApi19(Context context, Uri uri) {
    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 selection = MediaStore.Images.Media._ID + "=?";

    Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column,
            selection, new String[] { id }, null);

    int columnIndex = cursor.getColumnIndex(column[0]);

    if (cursor.moveToFirst()) {
        filePath = cursor.getString(columnIndex);
    }/*from w w  w. j  a  v  a 2s.co m*/
    cursor.close();
    return filePath;
}

From source file:com.hufeiya.SignIn.persistence.TopekaDatabaseHelper.java

/**
 * Gets a category from the given position of the cursor provided.
 *
 * @param cursor           The Cursor containing the data.
 * @param readableDatabase The database that contains the quizzes.
 * @return The found category.//from w  w  w . j  a v a2s  .  c om
 */
private static Category getCategory(Cursor cursor, SQLiteDatabase readableDatabase) {
    // "magic numbers" based on CategoryTable#PROJECTION
    final String id = cursor.getString(0);
    final String name = cursor.getString(1);
    final String themeName = cursor.getString(2);
    final Theme theme = Theme.valueOf(themeName);

    return new Category(name, id, theme);
}

From source file:Main.java

/**
 * Get a uri's file path//from   w  ww . ja  v a 2 s.  c om
 * 
 * @param context the application context
 * @param uri     the uri to query
 * 
 * @return the file path
 */
public static String getUriPath(Context context, Uri uri) {
    String filePath = null;

    String scheme = uri.getScheme();

    if (scheme.startsWith("content")) {
        String[] projection = { MediaStore.Files.FileColumns.DATA };

        /* 
         * FIXME 2013-10-24 Tianzi Hou
         * 
         * we cannot get file path if it is from 
         *  content://com.google.android.gallery3d.provider
         * i.e. the Picasa service
         */
        filePath = null;
        Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);

        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(projection[0]);
            cursor.moveToFirst();
            filePath = cursor.getString(column_index);
            cursor.close();
        }
    } else if (scheme.startsWith("file")) {
        filePath = uri.getPath();
    }

    return filePath;
}

From source file:Main.java

/**
 * /*from ww w.  jav  a 2s  .  c  o  m*/
 * @param context
 * @param uri
 *            uri of SCHEME_FILE or SCHEME_CONTENT
 * @return image path; uri will be changed to SCHEME_FILE
 */
public static String uriToImagePath(Context context, Uri uri) {
    if (context == null || uri == null) {
        return null;
    }

    String imagePath = null;
    String uriString = uri.toString();
    String uriSchema = uri.getScheme();
    if (uriSchema.equals(ContentResolver.SCHEME_FILE)) {
        imagePath = uriString.substring("file://".length());
    } else {// uriSchema.equals(ContentResolver.SCHEME_CONTENT)
        ContentResolver resolver = context.getContentResolver();
        Cursor cursor = resolver.query(uri, null, null, null, null);
        if (cursor.getCount() == 0) {
            Log.e(TAG, "Uri(" + uri.toString() + ") not found!");
            return null;
        }
        cursor.moveToFirst();
        imagePath = cursor.getString(1);
        // Change the SCHEME_CONTENT uri to the SCHEME_FILE.
        uri = Uri.fromFile(new File(imagePath));
    }
    Log.v(TAG, "Final uri: " + uri.toString());
    return imagePath;
}