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:net.peterkuterna.android.apps.devoxxsched.io.RemoteSessionsHandler.java

private static int isStarred(Uri uri, ContentResolver resolver) {
    final Cursor cursor = resolver.query(uri, SessionsQuery.PROJECTION, null, null, null);
    int starred = 0;
    try {//from   ww  w .j  a va2 s  .c o m
        if (cursor.moveToFirst()) {
            starred = cursor.getInt(SessionsQuery.STARRED);
        }
    } finally {
        cursor.close();
    }
    return starred;
}

From source file:Main.java

/**
 * Consolidates the file path determination functionality of the various
 * media prompts. Beginning with KitKat, the responses use a different
 * mechanism and needs a lot of special handling.
 *
 * @param ctxt//www.j av  a  2  s.c  om
 * @param uri
 * @param pathKey
 * @return
 */
@SuppressLint("NewApi")
public static String getPathFromUri(Context ctxt, Uri uri, String pathKey) {

    if (Build.VERSION.SDK_INT >= 19) {
        return getPath(ctxt, uri);
    } else {
        if (uri.toString().startsWith("file")) {
            return uri.toString().substring(7);
        } else {
            String[] projection = { pathKey };
            Cursor c = null;
            try {
                c = ctxt.getContentResolver().query(uri, projection, null, null, null);
                int column_index = c.getColumnIndexOrThrow(pathKey);
                String path = null;
                if (c.getCount() > 0) {
                    c.moveToFirst();
                    path = c.getString(column_index);
                }
                return path;
            } finally {
                if (c != null) {
                    c.close();
                }
            }
        }
    }
}

From source file:Main.java

public static File convertImageUriToFile(Uri imageUri, Activity activity) {
    Cursor cursor = null;
    try {/*from   w w w  .  j a  v  a  2  s .  co m*/
        String[] proj = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID,
                MediaStore.Images.ImageColumns.ORIENTATION };
        cursor = activity.managedQuery(imageUri, proj, null, null, null);
        int file_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        int orientation_ColumnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.ORIENTATION);
        if (cursor.moveToFirst()) {
            String orientation = cursor.getString(orientation_ColumnIndex);
            return new File(cursor.getString(file_ColumnIndex));
        }
        return null;
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

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

public static int getId(Context context) {
    Uri uri = getContentUri(context);//from   w  w  w.  j  a  v a2s .  com
    Cursor cursor = context.getContentResolver().query(uri, null, null, null, null);
    int id = 0;
    if (cursor != null && cursor.moveToFirst()) {
        id = cursor.getInt(0);
        cursor.close();
    }
    return id;
}

From source file:com.maskyn.fileeditorpro.util.AccessStorageApi.java

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

    if (uri == null || uri.equals(Uri.EMPTY))
        return "";

    String fileName = "";
    try {//w w w  .  j  a v  a 2 s . co m
        String scheme = uri.getScheme();
        if (scheme.equals("file")) {
            fileName = uri.getLastPathSegment();
        } else if (scheme.equals("content")) {
            String[] proj = { MediaStore.Images.Media.DISPLAY_NAME };
            Cursor cursor = context.getContentResolver().query(uri, proj, null, null, null);
            if (cursor != null && cursor.getCount() != 0) {
                int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DISPLAY_NAME);
                cursor.moveToFirst();
                fileName = cursor.getString(columnIndex);
            }
            if (cursor != null) {
                cursor.close();
            }
        }
    } catch (Exception ex) {
        return "";
    }
    return fileName;
}

From source file:mobisocial.noteshere.util.UriImage.java

public static float rotationForImage(Context context, Uri uri) {
    if (uri.getScheme().equals("content")) {
        String[] projection = { Images.ImageColumns.ORIENTATION };
        Cursor c = context.getContentResolver().query(uri, projection, null, null, null);
        try {//  ww w.  j a  v  a 2  s.c o  m
            if (c.moveToFirst()) {
                return c.getInt(0);
            }
        } finally {
            c.close();
        }
    } else if (uri.getScheme().equals("file")) {
        try {
            ExifInterface exif = new ExifInterface(uri.getPath());
            int rotation = (int) exifOrientationToDegrees(
                    exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL));
            return rotation;
        } catch (IOException e) {
            Log.e(TAG, "Error checking exif", e);
        }
    }
    return 0f;
}

From source file:Main.java

/**
 * Query the server app to get the file's display name.
 */// ww  w. j a  v  a  2s  .co m
public static String getUriFileName(Context context, Uri uri) {
    if (uri == null) {
        return null;
    }

    if (uri.getScheme().equals("file")) {
        return uri.getLastPathSegment();
    }

    if (uri.getScheme().equals("content")) {
        Cursor returnCursor = context.getContentResolver().query(uri, null, null, null, null);

        //Get the column index of the data in the Cursor,
        //move to the first row in the Cursor, get the data, and display it.
        int name_index = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
        //int size_index = returnCursor.getColumnIndex(OpenableColumns.SIZE);

        if (name_index < 0) {
            return null;
        }

        returnCursor.moveToFirst();

        //return returnCursor.getLong(size_index)
        return returnCursor.getString(name_index);
    }
    return null;
}

From source file:Main.java

@TargetApi(19)
public static File getFromMediaUri(Context context, ContentResolver resolver, Uri uri) {
    if (uri == null)
        return null;

    if (SCHEME_FILE.equals(uri.getScheme())) {
        return new File(uri.getPath());
    } else if (SCHEME_CONTENT.equals(uri.getScheme())) {
        String filePath = "";
        if (isMediaDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            Uri contentUri = null;//from  w  w  w .  j av a  2  s.c o  m
            if ("image".equals(type)) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            }
            final String selection = "_id=?";
            final String[] selectionArgs = new String[] { split[1] };

            Cursor cursor = null;
            final String column = "_data";
            final String[] projection = { column };

            try {
                cursor = context.getContentResolver().query(contentUri, projection, selection, selectionArgs,
                        null);
                if (cursor != null && cursor.moveToFirst()) {
                    final int column_index = cursor.getColumnIndexOrThrow(column);
                    filePath = cursor.getString(column_index);
                }
            } finally {
                if (cursor != null)
                    cursor.close();
            }
        } else {
            final String[] filePathColumn = { MediaStore.MediaColumns.DATA,
                    MediaStore.MediaColumns.DISPLAY_NAME };
            Cursor cursor = null;
            try {
                cursor = resolver.query(uri, filePathColumn, null, null, null);
                if (cursor != null && cursor.moveToFirst()) {
                    final int columnIndex = (uri.toString()
                            .startsWith("content://com.google.android.gallery3d"))
                                    ? cursor.getColumnIndex(MediaStore.MediaColumns.DISPLAY_NAME)
                                    : cursor.getColumnIndex(MediaStore.MediaColumns.DATA);
                    // Picasa images on API 13+
                    if (columnIndex != -1) {
                        filePath = cursor.getString(columnIndex);
                    }
                }
            } catch (IllegalArgumentException e) {
                // Google Drive images
                return getFromMediaUriPfd(context, resolver, uri);
            } catch (SecurityException ignored) {
                // Nothing we can do
            } finally {
                if (cursor != null)
                    cursor.close();
            }
        }

        if (!TextUtils.isEmpty(filePath)) {
            return new File(filePath);
        }
    }
    return null;
}

From source file:com.android.emailcommon.utility.AttachmentUtilities.java

/**
 * Resolve attachment id to content URI.  Returns the resolved content URI (from the attachment
 * DB) or, if not found, simply returns the incoming value.
 *
 * @param attachmentUri/*www  .  ja va2s  .  com*/
 * @return resolved content URI
 *
 * TODO:  Throws an SQLite exception on a missing DB file (e.g. unknown URI) instead of just
 * returning the incoming uri, as it should.
 */
public static Uri resolveAttachmentIdToContentUri(ContentResolver resolver, Uri attachmentUri) {
    Cursor c = resolver.query(attachmentUri, new String[] { Columns.DATA }, null, null, null);
    if (c != null) {
        try {
            if (c.moveToFirst()) {
                final String strUri = c.getString(0);
                if (strUri != null) {
                    return Uri.parse(strUri);
                }
            }
        } finally {
            c.close();
        }
    }
    return attachmentUri;
}

From source file:ee.ioc.phon.android.speak.Utils.java

/**
 * TODO: should we immediately return null if id = 0?
 *//*from www  .  jav a 2  s. c  o  m*/
public static String idToValue(Context context, Uri contentUri, String columnId, String columnUrl, long id) {
    String value = null;
    Cursor c = context.getContentResolver().query(contentUri, new String[] { columnUrl }, columnId + "= ?",
            new String[] { String.valueOf(id) }, null);

    if (c.moveToFirst()) {
        value = c.getString(0);
    }
    c.close();
    return value;
}