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:com.example.igorklimov.popularmoviesdemo.helpers.Utility.java

public static String getReviewText(Cursor cursor) {
    return cursor.getString(cursor.getColumnIndex(MovieContract.COLUMN_REVIEW_TEXT));
}

From source file:Main.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 ww w  . j a  v  a2s .  com
        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.d("android_utilities", "the error is " + e);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return null;
}

From source file:Main.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 ww  .java 2  s.  c om*/
        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) {
        //            FileLog.e("tmessages", e);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return null;
}

From source file:Main.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 {//  www .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);
            return cursor.getString(column_index);
        }
    } catch (Exception e) {
        Log.e("tmessages", e.getMessage());
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return null;
}

From source file:de.escoand.readdaily.DownloadHandler.java

public static float downloadProgress(final Context context, final String name) {
    Cursor cursor = Database.getInstance(context).getDownloads();
    long id = 0;/*from w  w w . j  a  va 2s  . c  o m*/
    float progress;

    // get download id
    while (cursor.moveToNext())
        if (cursor.getString(cursor.getColumnIndex(Database.COLUMN_SUBSCRIPTION)).equals(name)) {
            id = cursor.getLong(cursor.getColumnIndex(Database.COLUMN_ID));
            break;
        }
    cursor.close();
    if (id <= 0)
        return SUBSCRIPTION_DOWNLOAD_UNKNOWN;

    // get download
    cursor = ((DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE))
            .query(new DownloadManager.Query().setFilterById(id));
    if (!cursor.moveToFirst()) {
        Database.getInstance(context).removeDownload(id);
        return DOWNLOAD_ID_UNKNOWN;
    }

    // get progress
    progress = cursor.getFloat(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR))
            / cursor.getFloat(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));

    cursor.close();
    return progress;
}

From source file:Main.java

/**
 * Get the real file path from URI registered in media store 
 * @param contentUri URI registered in media store 
 *//*from   ww w . j ava2s  .c  om*/
public static String getRealPathFromURI(Activity activity, Uri contentUri) {

    String releaseNumber = Build.VERSION.RELEASE;

    if (releaseNumber != null) {
        /* ICS Version */
        if (releaseNumber.length() > 0 && releaseNumber.charAt(0) == '4') {
            String[] proj = { MediaStore.Images.Media.DATA };
            String strFileName = "";
            CursorLoader cursorLoader = new CursorLoader(activity, contentUri, proj, null, null, null);
            Cursor cursor = cursorLoader.loadInBackground();
            if (cursor != null) {
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                cursor.moveToFirst();
                if (cursor.getCount() > 0)
                    strFileName = cursor.getString(column_index);

                cursor.close();
            }
            return strFileName;
        }
        /* GB Version */
        else if (releaseNumber.startsWith("2.3")) {
            String[] proj = { MediaStore.Images.Media.DATA };
            String strFileName = "";
            Cursor cursor = activity.managedQuery(contentUri, proj, null, null, null);
            if (cursor != null) {
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

                cursor.moveToFirst();
                if (cursor.getCount() > 0)
                    strFileName = cursor.getString(column_index);

                cursor.close();
            }
            return strFileName;
        }
    }

    //---------------------
    // Undefined Version
    //---------------------
    /* GB, ICS Common */
    String[] proj = { MediaStore.Images.Media.DATA };
    String strFileName = "";
    Cursor cursor = activity.managedQuery(contentUri, proj, null, null, null);
    if (cursor != null) {
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

        // Use the Cursor manager in ICS         
        activity.startManagingCursor(cursor);

        cursor.moveToFirst();
        if (cursor.getCount() > 0)
            strFileName = cursor.getString(column_index);

        //cursor.close(); // If the cursor close use , This application is terminated .(In ICS Version)
        activity.stopManagingCursor(cursor);
    }
    return strFileName;
}

From source file:com.example.igorklimov.popularmoviesdemo.helpers.Utility.java

public static double getVote(Cursor c) {
    return Double.valueOf(c.getString(c.getColumnIndex(MovieContract.COLUMN_AVERAGE_VOTE)));
}

From source file:Main.java

public static String resolveFileName(Uri uri, Activity activity) {
    String filename = null;/*from  w  ww .j av  a2 s .co  m*/
    String uriString = uri.toString();

    if (uriString.startsWith("content://")) {
        Cursor cursor = null;
        try {
            cursor = activity.getContentResolver().query(uri, null, null, null, null);
            if (cursor != null && cursor.moveToFirst()) {
                String mimeType = MimeTypeMap.getSingleton()
                        .getExtensionFromMimeType(activity.getContentResolver().getType(uri));
                filename = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));

                if (mimeType != null && !filename.endsWith("." + mimeType)) {
                    filename += "." + mimeType;
                }
            }
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    } else if (uriString.startsWith("file://")) {
        filename = (new File(uriString)).getName();
    }
    return filename;
}

From source file:Main.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  .j  a va2  s.  co m*/
        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) {
        e.printStackTrace();
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return null;
}

From source file:Main.java

/**
 * Get {@link File} object for the given Android URI.<br>
 * Use content resolver to get real path if direct path doesn't return valid file.
 *///from  www .j  av a2s  .c  o  m
private static File getFileFromUri(Context context, Uri uri) {

    // first try by direct path
    File file = new File(uri.getPath());
    if (file.exists()) {
        return file;
    }

    // try reading real path from content resolver (gallery images)
    Cursor cursor = null;
    try {
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(uri, proj, null, null, null);
        if (cursor != null) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            String realPath = cursor.getString(column_index);
            file = new File(realPath);
        }
    } catch (Exception ignored) {
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }

    return file;
}