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 extractFilenameFromUri(Uri uri, Activity activity) {

    String fileName = null;//from w w  w .j a v  a 2 s.  co m
    if (uri == null) {
        throw new IllegalArgumentException();
    }
    String scheme = uri.getScheme();
    String path = uri.getPath();
    if (path != null && scheme != null && scheme.equals("file")) {
        fileName = path.substring(path.lastIndexOf("/") + 1);
    }

    String[] projection = { MediaStore.Images.ImageColumns.DISPLAY_NAME /* col1 */ };

    Cursor c = activity.managedQuery(uri, projection, null, null, null);
    if (c != null && c.moveToFirst()) {
        fileName = c.getString(0);
    }
    return fileName;
}

From source file:Main.java

public static void DeleteImageFromGalleryBase(final Context context, String fileName) {

    // Set up the projection (we only need the ID)
    String[] projection = { MediaStore.Images.Media._ID };

    // Match on the file path
    String selection = MediaStore.Images.Media.DATA + " = ?";
    String[] selectionArgs = new String[] { fileName };

    // Query for the ID of the media matching the file path
    Uri queryUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    ContentResolver contentResolver = context.getContentResolver();
    Cursor c = contentResolver.query(queryUri, projection, selection, selectionArgs, null);
    if (c.moveToFirst()) {
        // We found the ID. Deleting the item via the content provider will also remove the file
        long id = c.getLong(c.getColumnIndexOrThrow(MediaStore.Images.Media._ID));
        Uri deleteUri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
        contentResolver.delete(deleteUri, null, null);
    } else {// w  ww .j  av  a 2  s. com
        // File not found in media store DB
    }
    c.close();
}

From source file:Main.java

public static ArrayList<String> cursorToArrayList(Cursor cursor) {
    ArrayList<String> list = new ArrayList<>();
    if (cursor == null)
        return list;
    cursor.moveToFirst();

    while (!cursor.isAfterLast()) {
        list.add(cursor.getString(0));/*from  w  ww  . j a v a  2s  . c  o m*/
        cursor.moveToNext();
    }
    return list;
}

From source file:Main.java

public static String getRealPathFromURI(Context context, Uri contentURI) {
    String result = null;/*from   w ww. j a va 2 s  .  co  m*/
    Cursor cursor = context.getContentResolver().query(contentURI, null, null, null, null);
    if (cursor != null) {
        cursor.moveToFirst();
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
        result = cursor.getString(idx);
        cursor.close();
    }
    return result;
}

From source file:Main.java

final static public String GetContactNameByID(long contact_id, ContentResolver resolver) {
    if (resolver != null) {
        final Uri uri = Data.CONTENT_URI;

        final String[] columns = { ContactsContract.CommonDataKinds.StructuredName.FAMILY_NAME,
                ContactsContract.CommonDataKinds.StructuredName.GIVEN_NAME,
                ContactsContract.CommonDataKinds.StructuredName.MIDDLE_NAME
                //,ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME
                /*Data._ID,*/
                //Data.CONTACT_ID, 
                //Data.LOOKUP_KEY
        };/*from w  w  w.  j ava 2 s.c  o  m*/
        final String where = Data.CONTACT_ID + "=?" + " AND " + Data.MIMETYPE + "='"
                + StructuredName.CONTENT_ITEM_TYPE + "'";
        final String[] where_arg = { String.valueOf(contact_id) };

        Cursor cur = resolver.query(uri, columns, where, where_arg, null);
        if (cur.moveToFirst()) {
            String result = "";
            final String family_name = cur.getString(0);
            if (family_name != null)
                result += family_name;
            final String given_name = cur.getString(1);
            if (given_name != null)
                result += " " + given_name;
            final String middle_name = cur.getString(2);
            if (middle_name != null)
                result += " " + middle_name;
            //return String.format("%s %s %s",family_name,given_name,middle_name);
            return result;
        } //if(cur.moveToFirst())
    } //if(resolver!=null)
    return null;
}

From source file:Main.java

public static String getFileFromStorage(Context context, Intent data) {
    Uri pickedImage = data.getData();/*  w  w w  . ja v a  2s  . c  o  m*/
    String[] filePath = { MediaStore.Images.Media.DATA };
    String path = "";
    Cursor cursor = context.getContentResolver().query(pickedImage, filePath, null, null, null);
    if (cursor != null) {
        cursor.moveToFirst();
        path = cursor.getString(cursor.getColumnIndex(filePath[0]));
        cursor.close();
    }
    return path;
}

From source file:Main.java

public static int getContactIdFromPhoneNumber(final Context context, final String number) {
    final Uri uri = Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI, Uri.encode(number));
    final String[] projection = { PhoneLookup._ID };
    final Cursor c = context.getContentResolver().query(uri, projection, null, null, null);
    if (c.getCount() > 0) {
        c.moveToFirst();
        return c.getInt(0);
    } else {//from w w w.  ja v  a 2  s . c o  m
        return -1;
    }
}

From source file:Main.java

public static String[] getTrailers(Cursor data) {
    List<String> lista = new ArrayList<String>();
    lista.add("Select Trailer");

    data.moveToFirst();
    if (!data.getString(COL_TRAILER_KEY).equalsIgnoreCase("No Trailer Available")) {
        //loop through cursor to get all trailers
        for (int i = 1; i < data.getCount() + 1; i++) {
            if (!lista.contains(data.getString(COL_TRAILER_KEY))) {
                lista.add(data.getString(COL_TRAILER_KEY));
                data.moveToNext();//from   www.j a  v  a2  s.  c o  m
            } else {
                data.moveToNext();
            }
        }
    }

    String[] ltrailer;
    if (lista.size() < 1) {
        ltrailer = new String[1];
        ltrailer[0] = "No Trailer Available";
    } else {
        ltrailer = lista.toArray(new String[lista.size()]);
    }

    return ltrailer;
}

From source file:Main.java

/**
 * Calculates the amount of rotation needed for the image to look upright.
 * /*from   w ww .  j  av  a2  s.c  o  m*/
 * @param context
 *            the application's context
 * @param uri
 *            the Uri pointing to the file
 * @return the needed rotation as a <code>float</code>
 */
private static float rotationForImage(Context context, Uri uri) {
    if ("content".equals(uri.getScheme())) {
        String[] projection = { Images.ImageColumns.ORIENTATION };
        Cursor c = context.getContentResolver().query(uri, projection, null, null, null);
        if (c.moveToFirst()) {
            return c.getInt(0);
        }
    } else if ("file".equals(uri.getScheme())) {
        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

/**
 * Handles pre V19 uri's//  www .ja  va  2  s  .c o m
 * @param context
 * @param contentUri
 * @return
 */
private static String getPathForPreV19(Context context, Uri contentUri) {
    String res = null;

    String[] proj = { MediaStore.Images.Media.DATA };
    Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
    if (cursor != null) {
        if (cursor.moveToFirst()) {
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            res = cursor.getString(column_index);
        }
        cursor.close();
    }

    return res;
}