Example usage for android.database Cursor moveToFirst

List of usage examples for android.database Cursor moveToFirst

Introduction

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

Prototype

boolean moveToFirst();

Source Link

Document

Move the cursor to the first row.

Usage

From source file:Main.java

public static String getRealPathFromURI(Uri contentUri, Context ctx) {
    Log.d("thong", "Uri: " + contentUri.toString());

    try {/*from  w  ww .j a va  2  s .c  om*/
        String realpath = "";

        String[] proj = { MediaStore.Images.Media.DATA };

        //Cursor cursor = ((Activity) ctx).managedQuery(contentUri, proj, null, null, null);

        Cursor cursor = ctx.getContentResolver().query(contentUri, proj, null, null, null);

        Log.d("thong", "Column count: " + cursor.getColumnCount());

        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

        cursor.moveToFirst();

        realpath = cursor.getString(column_index);

        cursor.close();

        return realpath;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

/**
 * Get a file path from a Uri.//from w  w w  . jav  a2s .c om
 * 
 * @param context
 * @param uri
 * @return
 * @throws URISyntaxException
 * 
 * @author paulburke
 */
public static String getPath(Context context, Uri uri) throws URISyntaxException {

    if (DEBUG)
        Log.d(TAG + " File -",
                "Authority: " + uri.getAuthority() + ", Fragment: " + uri.getFragment() + ", Port: "
                        + uri.getPort() + ", Query: " + uri.getQuery() + ", Scheme: " + uri.getScheme()
                        + ", Host: " + uri.getHost() + ", Segments: " + uri.getPathSegments().toString());

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

        try {
            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) {
            // Eat it
        }
    }

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

    return null;
}

From source file:Main.java

public final static String getRealPathFromURI(Context context, Uri contentUri) {
    Cursor cursor = null;
    try {/*from   w  ww.  j a v a  2 s  . c o m*/
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:Main.java

public static String getPath(Context context, Uri contentUri) {
    Cursor cursor = null;
    try {// w  w  w .  ja va 2s.  co  m
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

From source file:Main.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  v a 2 s .  c om
 * @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) {

    final String[] projection = { MediaStore.MediaColumns.DATA };
    final Cursor cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);

    if (cursor != null) {
        try {

            if (cursor.moveToFirst()) {
                return cursor.getString(0);
            }
        } finally {
            cursor.close();
        }
    }
    return null;
}

From source file:Main.java

static String convertUriToPath(Context context, Uri uri) {
    Log.v(TAG, "convertUriToPath : " + uri + " @" + context);

    String path = null;/*from www  . j  a  v a2 s .  co m*/
    if (null != uri) {
        String scheme = uri.getScheme();
        if (null == scheme || scheme.equals("") || scheme.equals(ContentResolver.SCHEME_FILE)) {
            path = uri.getPath();
        } else if (scheme.equals("http")) {
            path = uri.toString();
        } else if (scheme.equals(ContentResolver.SCHEME_CONTENT)) {
            String[] projection = new String[] { MediaStore.MediaColumns.DATA };
            Cursor cursor = null;
            try {
                cursor = context.getContentResolver().query(uri, projection, null, null, null);
                if (null == cursor || 0 == cursor.getCount() || !cursor.moveToFirst()) {
                    throw new IllegalArgumentException("Given Uri could not be found in media store");
                }
                int pathIndex = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
                path = cursor.getString(pathIndex);
            } catch (SQLiteException e) {
                throw new IllegalArgumentException(
                        "Given Uri is not formatted in a way so that it can be found in media store.");
            } finally {
                if (null != cursor) {
                    cursor.close();
                }
            }
        } else {
            throw new IllegalArgumentException("Given Uri scheme is not supported");
        }
    }

    Log.v(TAG, "convertUriToPath : >" + path);
    return path;
}

From source file:Main.java

public static String loadString(String key, String defaultVal, SQLiteDatabase db) {
    String ret = defaultVal;//from  w  ww. ja v  a2  s. co  m
    if (db == null)
        return ret;

    Cursor cursor = db.query(TABLE_SETTINGS,
            new String[] { KEY_SETTINGS_ID, KEY_SETTINGS_KEY, KEY_SETTINGS_VALUE }, KEY_SETTINGS_KEY + "=?",
            new String[] { key }, null, null, null, null);

    if (cursor != null) {
        boolean moveFirst = cursor.moveToFirst();
        if (moveFirst)
            ret = cursor.getString(2);
    }

    return ret;
}

From source file:Main.java

public static String queryAudioName(Context context, Uri name) {
    String audioId;/*from  ww  w .j  a  v  a  2s  .  co m*/
    String uriName = name.toString();
    uriName = uriName.substring(uriName.lastIndexOf("/") + 1);
    Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null,
            null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
    if (cursor != null && cursor.getCount() > 0) {
        cursor.moveToFirst();
        int counter = cursor.getCount();
        for (int j = 0; j < counter; j++) {
            audioId = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media._ID));

            if (uriName.equals(audioId)) {
                uriName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
            } else {
                Ringtone ringtone = RingtoneManager.getRingtone(context, name);
                uriName = ringtone.getTitle(context);
                break;
            }
            cursor.moveToNext();

        }
        cursor.close();
    } else {
        try {
            Ringtone ringtone = RingtoneManager.getRingtone(context, name);
            uriName = ringtone.getTitle(context);
        } catch (Exception e) {
            return uriName;
        }
    }
    return uriName;
}

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 {//  w  w w . ja  v a  2  s . co 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;
}

From source file:Main.java

public static File getRealFileFromURI(Context context, Uri contentUri) {
    Cursor cursor = null;
    try {/*from  w w w . ja v  a  2 s.c  o m*/
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String path = cursor.getString(column_index);
        return new File(path);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}